Jquery结构核心源码

 1 (function( window, undefined ) {
 2    
 3     var jQuery = (function() {
 4        // 构建jQuery对象
 5        var jQuery = function( selector, context ) {
 6            return new jQuery.fn.init( selector, context, rootjQuery );
 7        }
 8    
 9        // jQuery对象原型
10        jQuery.fn = jQuery.prototype = {
11            constructor: jQuery,
12            init: function( selector, context, rootjQuery ) {
13               // selector有以下7种分支情况:
14               // DOM元素
15               // body(优化)
16               // 字符串:HTML标签、HTML字符串、#id、选择器表达式
17               // 函数(作为ready回调函数)
18               // 最后返回伪数组
19            }
20        };
21    
22        // Give the init function the jQuery prototype for later instantiation
23        jQuery.fn.init.prototype = jQuery.fn;
24    
25        // 合并内容到第一个参数中,后续大部分功能都通过该函数扩展
26        // 通过jQuery.fn.extend扩展的函数,大部分都会调用通过jQuery.extend扩展的同名函数
27        jQuery.extend = jQuery.fn.extend = function() {};
28       
29        // 在jQuery上扩展静态方法
30        jQuery.extend({
31            // ready bindReady
32            // isPlainObject isEmptyObject
33            // parseJSON parseXML
34            // globalEval
35            // each makeArray inArray merge grep map
36            // proxy
37            // access
38            // uaMatch
39            // sub
40            // browser
41        });
42  
43         // 到这里,jQuery对象构造完成,后边的代码都是对jQuery或jQuery对象的扩展
44        return jQuery;
45    
46     })();
47    
48     window.jQuery = window.$ = jQuery;
49 })(window);
 1 // 合并两个或更多对象的属性到第一个对象中,jQuery后续的大部分功能都通过该函数扩展
 2 // 通过jQuery.fn.extend扩展的函数,大部分都会调用通过jQuery.extend扩展的同名函数
 3  
 4 // 如果传入两个或多个对象,所有对象的属性会被添加到第一个对象target
 5  
 6 // 如果只传入一个对象,则将对象的属性添加到jQuery对象中。
 7 // 用这种方式,我们可以为jQuery命名空间增加新的方法。可以用于编写jQuery插件。
 8 // 如果不想改变传入的对象,可以传入一个空对象:$.extend({}, object1, object2);
 9 // 默认合并操作是不迭代的,即便target的某个属性是对象或属性,也会被完全覆盖而不是合并
10 // 第一个参数是true,则会迭代合并
11 // 从object原型继承的属性会被拷贝
12 // undefined值不会被拷贝
13 // 因为性能原因,JavaScript自带类型的属性不会合并
14  
15 // jQuery.extend( target, [ object1 ], [ objectN ] )
16 // jQuery.extend( [ deep ], target, object1, [ objectN ] )
17 jQuery.extend = jQuery.fn.extend = function() {
18     var options, name, src, copy, copyIsArray, clone,
19        target = arguments[0] || {},
20        i = 1,
21        length = arguments.length,
22        deep = false;
23  
24     // Handle a deep copy situation
25     // 如果第一个参数是boolean型,可能是深度拷贝
26     if ( typeof target === "boolean" ) {
27        deep = target;
28        target = arguments[1] || {};
29        // skip the boolean and the target
30        // 跳过boolean和target,从第3个开始
31        i = 2;
32     }
33  
34     // Handle case when target is a string or something (possible in deep copy)
35     // target不是对象也不是函数,则强制设置为空对象
36     if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
37        target = {};
38     }
39  
40     // extend jQuery itself if only one argument is passed
41     // 如果只传入一个参数,则认为是对jQuery扩展
42     if ( length === i ) {
43        target = this;
44        --i;
45     }
46  
47     for ( ; i < length; i++ ) {
48        // Only deal with non-null/undefined values
49        // 只处理非空参数
50        if ( (options = arguments[ i ]) != null ) {
51            // Extend the base object
52            for ( name in options ) {
53               src = target[ name ];
54               copy = options[ name ];
55  
56               // Prevent never-ending loop
57               // 避免循环引用
58               if ( target === copy ) {
59                   continue;
60               }
61  
62               // Recurse if we're merging plain objects or arrays
63               // 深度拷贝且值是纯对象或数组,则递归
64               if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
65                   // 如果copy是数组
66                   if ( copyIsArray ) {
67                      copyIsArray = false;
68                      // clone为src的修正值
69                      clone = src && jQuery.isArray(src) ? src : [];
70                   // 如果copy的是对象
71                   } else {
72                      // clone为src的修正值
73                      clone = src && jQuery.isPlainObject(src) ? src : {};
74                   }
75  
76                   // Never move original objects, clone them
77                   // 递归调用jQuery.extend
78                   target[ name ] = jQuery.extend( deep, clone, copy );
79  
80               // Don't bring in undefined values
81               // 不能拷贝空值
82               } else if ( copy !== undefined ) {
83                   target[ name ] = copy;
84               }
85            }
86        }
87     }
88  
89     // Return the modified object
90     // 返回更改后的对象
91     return target;
92 };

 

posted @ 2014-11-22 17:41  至道中和  阅读(169)  评论(0)    收藏  举报