jquery源码分析笔记:jQuery.extend
基于jQuery-2.1.4,一切尽在注释中呀
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
/*
*判断是否为深复制或者浅复制,$.extend(true|false,target,src);
*/
if ( typeof target === "boolean" ) {
deep = target;
// Skip the boolean and the target
//如果第一个参数是boolean值那么第二个参数就是复制后的目标,i = 1;
target = arguments[ i ] || {};
i++;
}
// Handle case when target is a string or something (possible in deep copy)
/*
* 如果复制目标既不是对象也不是方法(是一个字符串或者其他。。),就变为一个空对象;如:$.extend(undefined|'',src);
*/
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
// Extend jQuery itself if only one argument is passed
/*
* 扩展jQuery自身插件,当只有一个参数的时候;i = 1,length = 1;jquery插件开发一般基于两种方式,1:基于类;2:基于对象;
* 基于类 $.sayHello() 基于对象 $().sayHello;
* $.extend({ $.fn.extend({ sayHello: 'hello word' })
* sayHello:function(){
* alert('hello');
* }
* })
*/
if ( i === length ) {
target = this;
i--; //i = 0;
}
//循环复制开始
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
//判断值是否为null|undefined
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
//for in遍历对象属性
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
/*防止死循环复制,$.extend(name,{obj:name})
* 所以是 target 跟 copy比较,而不是src跟copy比较
*/
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
//递归遍历复制,当属性值也是对象时,,如$.extend(true,{name:'zhangsan'},{student:[{name:'jack',age:23},{name:'tontjon',age:43}]})
target[ name ] = jQuery.extend( deep, clone, copy );
// Don't bring in undefined values
//浅复制的时候,跳过undefined
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
}; 相关推荐
瓜牛呱呱 2020-11-12
柳木木的IT 2020-11-04
yifouhu 2020-11-02
lei0 2020-11-02
源码zanqunet 2020-10-26
码代码的陈同学 2020-10-14
lukezhong 2020-10-14
clh0 2020-09-18
changcongying 2020-09-17
星辰大海的路上 2020-09-13
abfdada 2020-08-26
mzy000 2020-08-24
shenlanse 2020-08-18
zhujiangtaotaise 2020-08-18
xiemanR 2020-08-17