这篇文章说得很好,https://www.cnblogs.com/SheilaSun/p/4779895.html
重点在于jQuery对象是怎么创建的:
jQuery =
function( selector, context )
{
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
}
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
}
这里是new 了一个对象,所以每次使用$()都是一个新的对象
jQuery.fn = jQuery.prototype = {
//fn即对应prototype
constructor : jQuery,
init: function( selector, context, rootjQuery ) {
...
return this ;
}
...
}
jQuery.fn.init.prototype = jQuery.fn;
constructor : jQuery,
init: function( selector, context, rootjQuery ) {
...
return this ;
}
...
}
jQuery.fn.init.prototype = jQuery.fn;
jQuery是jQuery.prototype.init new的对象,new出来的对象[[prototype]]指向它的原型,也就是jQuery.prototype(根据上面一句赋值得出)
init 是jQuery.prototype的方法,
jQuery可以访问Query.prototype上的方法,因为init可以获取到这些方法。