//1. 扩展插件 $.extend = $.fn.extend({a: function() {}}); 只能有1个参数 target = this;
//2. 浅层克隆 $.extend(obj1, obj2, obj3...); 至少2个参数 target = arguments[0];
//3. 深度克隆 $.extend(true, obj1, obj2, obj3...); 至少3个参数 target = arguments[1];
//never-ending loop 循环引用
//jQuery.extend ---> $(). ---> JQuery.protype
jQuery.extend = jQuery.fn.extend = function() {
var options, name, src, copy, copyIsArry, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
if (typeof target === "boolean") {
deep = target;
target = arguments[i] || {};
i++;
}
if (typeof target !== "object" && !jQuery.isFunction(target)) {
target = {};
}
if (i === length) {
target = this;
i--;
}
for (; i < length; i++) {
if (options = arguments[i] != null) {
for (name in options) {
src = target[name];
copy = options;
if (target === copy) {
continue;
}
if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArry = jQuery.isArray(cppy)))) {
if (copyIsArry) {
copyIsArry = false;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
target[name] = jQuery.extend(deep, clone, copy);
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target;
};