1,自调用的匿名函数
(function( window, undefined ) {
// jquery code
})(window);
说明:
一,这是一个自调用匿名函数。在第一个括号内,创建一个匿名函数;第二个括号,立即执行, 作用:创建了一个“私有”的命名空间,该命名空间的变量和方法,不会破坏全局的命名空间。
二 ,通过传入window变量,使得window由全局变量变为局部变量,当在jQuery代码块中访问window时,不需要将作用域链回退到顶层作用域,这样可以更快的访问window;
三,在 自调用匿名函数 的作用域内,确保undefined是真的未定义。因为undefined能够被重写,赋予新的值
2,主要结构
(function( window, undefined ) {
var // 调用jquery.fn,init(),构造jQuery对象
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
},
//定义jquery对象原型
jQuery.fn = jQuery.prototype = {
//..
init: function( selector, context, rootjQuery ) {
// selector有以下7种分支情况:
// DOM元素
// body(优化)
// 字符串:HTML标签、HTML字符串、#id、选择器表达式
// 函数(作为ready回调函数)
// 最后返回伪数组
},
//jquery对象原型的其他属性
}
// Give the init function the jQuery prototype for later instantiation
//简写,减少查找次数
jQuery.fn.init.prototype = jQuery.fn;
// 合并内容到第一个参数中,后续大部分功能都通过该函数扩展
// 通过jQuery.fn.extend扩展的函数,大部分都会调用通过jQuery.extend扩展的同名函数
jQuery.extend = jQuery.fn.extend = function() {};
//以后代码通过jQuery.extend({})扩展jquery的方法,通过jQuery.fn.extend({})扩展jQuery对象的属性,方法
})( window );
- jQuery对象不是通过 new jQuery 创建的,而是通过 new jQuery.fn.init 创建的
2.
先执行 jQuery.fn = jQuery.prototype,再执行 jQuery.fn.init.prototype = jQuery.fn;
jQuery.fn.init.prototype =jQuery.fn ,以后jQuery.fn上的方法都会挂在到 jQuery.fn.init()方法返回的对象上,也会挂在到通过 :
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
}
创建的jQuery对象上(例如:$("div")).
3. 扩展实现, 通过定义
jQuery.extend = jQuery.fn.extend =
function
(){};
实现.
此函数功能:
合并两个或更多对象的属性到第一个对象中,jQuery后续的大部分功能都通过该函数扩展,
如果传入两个或多个对象,所有对象的属性会被添加到第一个对象target
如果只传入一个对象,则将对象的属性添加到jQuery对象(jQuery.extend:jQuery全局函数,jQuery.fn.extend:jQuery对象方法)中
用这种方式,我们可以为jQuery命名空间增加新的方法。可以用于编写jQuery插件。
默认合并操作是不迭代的,即便target的某个属性是对象或属性,也会被完全覆盖而不是合并
第一个参数是true,则会迭代合并
从object原型继承的属性会被拷贝
undefined值不会被拷贝
函数注释,原文地址:http://jsdashi.com/develop/jquery-1.9/:
/**
扩展属性
* @param {Object} arguments[0] 扩展对像 如果为布尔值表示是否深度扩展
* @param {Object} arguments[1] 原始对像,可以传入多个原始对象
* @return {Object} 返回扩展后的对像
*/
jQuery.extend = jQuery.fn.extend = function () {
var src, copyIsArray, copy, name, options, clone,
target = arguments[0] || {},//扩展对象
i = 1,
length = arguments.length,
deep = false;// deep:深度扩展
// Handle a deep copy situation
// 检测是否需要深度扩展
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== "object" && !jQuery.isFunction(target)) {
target = {};
}
// extend jQuery itself if only one argument is passed
if (length === i) {
target = this;
--i;
}
for (; i < length; i++) {
// Only deal with non-null/undefined values
// options:非空扩展对象
if ((options = arguments[i]) != null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging plain objects or arrays
//递归,扩展数组或者对象
if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) {
if (copyIsArray) {
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[name] = jQuery.extend(deep, clone, copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
// Return the modified object
// 返回扩展后的对象
return target;
};