阅读背景:

jQuery源代码学习笔记:jQuery.fn.init(selector,context,rootjQuery)代码具体解释

来源:互联网 

3.1 源代码

init: function( selector, context, rootjQuery ) {
		var match, elem, ret, doc;

		// Handle $(""), $(null), or $(undefined)
        //假设selector为空格。!selector为false
		if (!selector) {
            //此时this为空jQuery对象
			return this;
		}

		// Handle $(DOMElement)
        //nodeType节点类型。利用是否有nodeType属性来推断是否是DOM元素
		if ( selector.nodeType ) {
            //将第一个元素和属性context指向selector
			this.context = this[0] = selector;
			this.length = 1;
			return this;
		}

		// The body element only exists once, optimize finding it
        //由于body仅仅出现一次。利用!context进行优化
		if ( selector === "body" && !context && document.body ) {
            //context指向document对象
			this.context = document;
			this[0] = document.body;
			this.selector = selector;
			this.length = 1;
			return this;
		}

		// Handle HTML strings
		if ( typeof selector === "string" ) {
		    // Are we dealing with HTML string or an ID?init: function( selector, context,



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: