jQuery.extend = jQuery.fn.extend = function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// 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
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;
};
extend,可以用于jq自身的扩展,也可以用于对象拷贝
使用方法:
1、给JQ扩展方法
2、对象的浅拷贝
3、对象的深拷贝
ps:拷贝的参数可以有多个,如 $.extend(a,b,c,d...)
a后面的全部拷贝给a
代码分析:
var options, name, src, copy, copyIsArray, clone,
options:arguments参数
name:枚举k值
src:以上示例中的a.a,当a.a不存在,则为undefined
copy:以上实例中的b.b
copyIsArray:用来判断是不是数组
clone:对src的复制
1、对自身的拓展
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
target:要赋值的对象
对自身的拓展,length肯定为1,和初始设置的i相等,则把i设置为0,后面for循环一次;
if ( length === i ) {
target = this;
--i;
}
for循环直接走这里
if ( copy !== undefined ) {
target[ name ] = copy;
}
2、浅拷贝
第一个参数boolean设置为false,因为执行代码执行默认为浅拷贝:deep=false,第一个参数省略。传入参数至少为2个以上,$.extend(a,b,c,d...)
//这个判断是为了格式化无效参数
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
for循环里面走的流程和对自身拷贝一样
3、深拷贝
第一个参数肯定为true,for循环索引变为从2开始,索引1的值的赋值的对象,上列中的a。
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
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
//深拷贝 && copy有值 && copy为{} / new Object对象或者数组
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;
}
}
对arguments参数的判断然后分别做出执行,这种方法很值得学习。