jquery源码-jquery()

阅读更多
源码95~289行,jquery.fn核心函数(utils)
jQuery.fn = jQuery.prototype = {
	constructor: jQuery,
	init: function( selector, context, rootjQuery ) {},//下面单独讲

	// Start with an empty selector
	selector: "",

	// The current version of jQuery being used
	jquery: "1.8.2",//当前版本

	// The default length of a jQuery object is 0
	length: 0,//定义jquery初始化对象的长度

	// The number of elements contained in the matched element set
	size: function() {},//返回当前对象长

	toArray: function() {},//数组转换

	// Get the Nth element in the matched element set OR
	// Get the whole matched element set as a clean array
	get: function( num ) {},//获取jquery对象中的第num+1个元素

	// Take an array of elements and push it onto the stack
	// (returning the new matched element set)
	pushStack: function( elems, name, selector ) {},//将DOM集合元素elems添加jquery栈中

	// Execute a callback for every element in the matched set.
	// (You can seed the arguments with an array of args, but this is
	// only used internally.)
	each: function( callback, args ) {},//见jQuery.each

	ready: function( fn ) {},//见jQuery.ready.promise()

	eq: function( i ) {},//返回第i+1个元素

	first: function() {},//返回第一个元素

	last: function() {},//返回最后一个元素

	slice: function() {},

	map: function( callback ) {},

	end: function() {},

	// For internal use only.
	// Behaves like an Array's method, not like a jQuery method.
	push: core_push,
	sort: [].sort,
	splice: [].splice
};

// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;




init方法

init方法实际就是juqery的主函数 $()或jQuery()
$()参数:
   selector: 选择器(支持字符串,dom元素,数组,函数,jQuery对象,null,undefined,false)  
   content : 一个待查找的 DOM 元素集、文档或 jQuery 对象。
   rootjQuery : 当前文档作用域的根节点


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

		// Handle $(""), $(null), $(undefined), $(false)
		if ( !selector ) {
			return this;
		}

		// Handle $(DOMElement) 一个参数,且为DOM元素 $(body)
		if ( selector.nodeType ) {
			this.context = this[0] = selector;
			this.length = 1;
			return this;
		}

		// Handle HTML strings //html字符串
		if ( typeof selector === "string" ) {//''形式字符串 如 $('')
			if ( selector.charAt(0) === "<" && selector.charAt( 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 {//含html的字符串或含#id形式的字符串
                                
				match = rquickExpr.exec( selector );
			}

			// Match html or make sure no context is specified for #id
			if ( match && (match[1] || !context) ) {
                                //如 $("

Hello

") // HANDLE: $(html) -> $(array) if ( match[1] ) { context = context instanceof jQuery ? context[0] : context; doc = ( context && context.nodeType ? context.ownerDocument || context : document ); // scripts is true for back-compat selector = jQuery.parseHTML( match[1], doc, true ); if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) { this.attr.call( selector, context, true ); } return jQuery.merge( this, selector ); // HANDLE: $(#id) } else { elem = document.getElementById( match[2] ); // Check parentNode to catch when Blackberry 4.6 returns // nodes that are no longer in the document #6963 if ( elem && elem.parentNode ) { // Handle the case where IE and Opera return items // by name instead of ID if ( elem.id !== match[2] ) { return rootjQuery.find( selector ); } // Otherwise, we 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: $(function) // $(function(){ //Document is ready //}); // Shortcut for document ready } else if ( jQuery.isFunction( selector ) ) { return rootjQuery.ready( selector ); } if ( selector.selector !== undefined ) { this.selector = selector.selector; this.context = selector.context; } return jQuery.makeArray( selector, this ); }


1.jQuery()或$()
//Handle $(""), $(null), $(undefined), $(false), $()
if ( !selector ) {
	return this;//返回该对象
}

2.jQuery(element)或$(element)
// Handle $(DOMElement) 一个参数,且为DOM元素 $(body)
if ( selector.nodeType ) {
	this.context = this[0] = selector;//设置该对象的context属性为该DOM元素
	this.length = 1;
	return this;
}

3.jQuery('')或$('')
// Handle $('',document.forms[0]) 
// 匹配类似"ddd新浪网asd" 格式,返回match[1]的值为"新浪网"
if ( match[1] ) {
    context = context instanceof jQuery ? context[0] : context;//context参数是否jQuery实例
    //获取上下文,当上下文为DOM元素时,则为根节点,否则为document文档
    doc = ( context && context.nodeType ? context.ownerDocument || context : document );

    // scripts is true for back-compat
    selector = jQuery.parseHTML( match[1], doc, true );
    if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
        this.attr.call( selector, context, true );
    }
    return jQuery.merge( this, selector );
}else{
 //
}

4.jQuery(#id)或$(#id)
{
    //match[2]为匹配#id的格式,如果match[2]不为空则返回id的字符表示,如“#name”则match[2]值为“name”
    elem = document.getElementById( match[2] );

    // Check parentNode to catch when Blackberry 4.6 returns
    // nodes that are no longer in the document #6963
    if ( elem && elem.parentNode ) {
        // Handle the case where IE and Opera return items
        // by name instead of ID
        //由于IE和Opera浏览器在调用getElementById方法时用会同时把name相同的属性找出来,所以针对这中情况就利用name属性方式查找
        if ( elem.id !== match[2] ) {
            return rootjQuery.find( selector );
        }

        // Otherwise, we inject the element directly into the jQuery object
        this.length = 1;
        this[0] = elem;
    }

    this.context = document;
    this.selector = selector;
    return this;
}


5.jQuery(callback)或$(callback)
   return rootjQuery.ready( selector );//当参数类型为function时

6.jQuery(expr, $(...))或$(expr, $(...)),如:$("div>p",$(...)),$("input:radio",document.forms[0]),$("div.foo",document.forms[0])
if ( !context || context.jquery ) {
   return ( context || rootjQuery ).find( selector  );//
}


7.以上所有情况外的其他情况
    // HANDLE: $(expr, context)
    return this.constructor( context ).find( selector );

你可能感兴趣的:(jquery,javascript,jquery1.8.2)