当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery最基本的功能实现方法

jQuery最基本的功能实现方法

2018年11月25日  | 移动技术网IT编程  | 我要评论

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;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网