本文是在阅读了Aaron艾伦的jQuery源码解析(地址:http://www.imooc.com/learn/172)后的个人体会以及笔记。在这里感谢艾伦老师深入浅出的讲解!!
先来看看如何生成一个jQuery对象,源码:
var jQuery = function( selector, context ) { return new jQuery.fn.init( selector, context ); };
当我们使用jQuery('something')或者$('something')时,我们得到的是一个 jQuery.fn.init()对象。那么jQuery.fn是什么鬼?
jQuery.fn = jQuery.prototype = { // jQuery版本 jquery: version, constructor: jQuery, // 构造函数 // Start with an empty selector selector: "", // The default length of a jQuery object is 0 length: 0, // 省略..... }
jQuery.fn 实际上是jQuery构造函数的原型对象的引用!! 所以我们以后看到 jQuery.fn时,把他当成jQuery构造函数的原型对象就可以了。
知道了jQuery.fn , 接下来看看jQuery.fn.init()函数
jQuery.fn.init = function( selector, context ) { // 省略....
return this; };
jQuery.fn.init.prototype = jQuery.prototype; // 注意这里! 这句代码让init对象可以使用jQuery的原型方法。
这样,我们在创建jQuery对象时就不用使用new关键字了。
整体看一下源码架构:
var $ = jQuery = function(selector,context){ return new jQuery.fn.init(selector,context) // 返回一个jQuery.fn.init()对象 } jQuery.fn = jQuery.prototype = { constructor:jQuery, init:function(){ // 省略..... return this; } } jQuery.fn.init.prototype = jQuery.fn
直观的感受一下相互之间的关系:
调用jQuery函数,我们得到的是一个jQuery.fn.init实例,这个实例的原型对象被重新指向到了jQuery函数的原型对象,所以这个实例可以使用jQuery原型对象的属性和方法,而如果我们给jQuery函数附加方法,那么这个方法就变成了静态方法。
然后来看一下jQuery.fn.init函数的源码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var rootjQuery, // A simple way to check for HTML strings // Prioritize #id over <tag> to avoid XSS via location.hash (#9521) // Strict HTML recognition (#11290: must start with <) rquickExpr = /^(?:s*(<[wW]+>)[^>]*|#([w-]*))$/, // <any+>--任意个非右尖括号字符 或者 以#开头的 init = jQuery.fn.init = function( selector, context ) { var match, elem; // HANDLE: $(""), $(null), $(undefined), $(false) if ( !selector ) { return this; } // Handle HTML strings if ( typeof selector === "string" ) { // 如果selector的格式是字符串类型,且字符串长度大于等于3,并且内容格式为: <something> if ( selector[0] === "<" && selector[ selector.length - 1 ] === ">" && selector.length >= 3 ) { // Assume that strings that start and end with <> are HTML and skip the regex check match = [ null, selector, null ]; } else { match = rquickExpr.exec( selector ); } // Match html or make sure no context is specified for #id if ( match && (match[1] || !context) ) { // HANDLE: $(html) -> $(array) if ( match[1] ) { // context = context instanceof jQuery ? context[0] : context; // Option to run scripts is true for back-compat // Intentionally let the error be thrown if parseHTML is not present jQuery.merge( this, jQuery.parseHTML( match[1], context && context.nodeType ? context.ownerDocument || context : document, true ) ); // HANDLE: $(html, props) if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) { for ( match in context ) { // Properties of context are called as methods if possible if ( jQuery.isFunction( this[ match ] ) ) { this[ match ]( context[ match ] ); // ...and otherwise set as attributes } else { this.attr( match, context[ match ] ); } } } return this; // HANDLE: $(#id) } else { elem = document.getElementById( match[2] ); // Support: Blackberry 4.6 // gEBID returns nodes no longer in the document (#6963) if ( elem && elem.parentNode ) { // Inject the element directly into the jQuery object this.length = 1; this[0] = elem; } this.context = document; this.selector = selector; return this; } // HANDLE: $(expr, $(...)) } else if ( !context || context.jquery ) { return ( context || rootjQuery ).find( selector ); // HANDLE: $(expr, context) // (which is just equivalent to: $(context).find(expr) } else { return this.constructor( context ).find( selector ); } // HANDLE: $(DOMElement) } else if ( selector.nodeType ) { this.context = this[0] = selector; this.length = 1; return this; // HANDLE: $(function) // Shortcut for document ready } else if ( jQuery.isFunction( selector ) ) { return typeof rootjQuery.ready !== "undefined" ? rootjQuery.ready( selector ) : // Execute immediately if ready is not present selector( jQuery ); } if ( selector.selector !== undefined ) { this.selector = selector.selector; this.context = selector.context; } return jQuery.makeArray( selector, this ); };
配张思路图: