// Define a local copy of jQueryjQuery = function( selector, context ) {// The jQuery object is actually just the init constructor 'enhanced'return new jQuery.fn.init( selector, context, rootjQuery );}jQuery.fn = jQuery.prototype = {...init: function( selector, context, rootjQuery ) {}...}// Give the init function the jQuery prototype for later instantiationjQuery.fn.init.prototype = jQuery.fn;
其中
jQuery = function( selector, context ) {// The jQuery object is actually just the init constructor 'enhanced'return new jQuery.fn.init( selector, context, rootjQuery );}定义了jQuery变量就是一个方法,方法体就是:
function( selector, context ) {// The jQuery object is actually just the init constructor 'enhanced'return new jQuery.fn.init( selector, context, rootjQuery );}所以在外部我们调用$("selector")等价于调用
new jQuery.fn.init( selector, context, rootjQuery );
返回的就是jQuery.fn.init的一个实例。
由于
jQuery.fn = jQuery.prototypejQuery.fn.init.prototype = jQuery.fn
所以
jQuery.fn.init.prototype = jQuery.prototype.init.prototype = jQuery.prototype所以jQuery.fn.init的实例的prototype就是jQuery.prototype,因此会继承jQuery.prototype上的属性和方法。
jquery源码学习2
最新推荐文章于 2018-08-17 17:01:27 发布