jquery最基本的功能实现方法。
1.实现变量私有化--->使用立即执行函数实现
2. 把变量暴露到全局 ---> 利用对象,把变量添加到window的属性,使用window[prop]
为了能够使用$ 和 jquery ----> window.jquery = window.$ = jquery;
3. 不需要 new 构造函数
① 返回一个构造函数
② 将函数定义在jquery的原型上
③ 入口函数init的原型指向jquery的原型,实现链式调用 $.fn.init.prototype = $.prototype;
var jquery = function(){
return new jquery.prototype.init();
}
window.$ = window.jquery = jquery;
$.fn = $.prototype = {
init : function(){
this[0] = document.getelementbyclassname('box')[0];
length = 1;
return this;
},
css : function (){
console.log('css')
},
html : function (){
console.log('html');
return this;
}
}
4. 实现链式调用
原型上的每个构造函数都return this;
如对本文有疑问, 点击进行留言回复!!
同事牛逼啊,写了个隐藏 bug,我排查了 3 天才解决问题!
【JavaScript笔记(一)】万丈高楼平地起 - 基本概念篇
轻松解决 org.apache.taglibs.standard.tlv.JstlCoreTLV 困惑
网友评论