欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery最基本的功能实现方法

程序员文章站 2023-11-15 19:18:28
jquery最基本的功能实现方法。 1.实现变量私有化--->使用立即执行函数实现 2. 把变量暴露到全局 ---> 利用对象,把变量添加到window的属性,使用window[prop...

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;