我试图理解jquery扩展函数,基本上看看下面的代码:
function () {
var src, copyIsArray, copy, name, options, clone, target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
// skip the boolean and the target
target = arguments[i] || {};
i++;
}
// 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 (i === length) {
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;
}
现在我理解代码的大部分内容都需要if
检查I.E.以下代码行:
if (target === copy) {
continue;
}
这条线到底在检查什么?
如果在此检查之前仔细查看几行代码,可以看到下面的::
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;
}
为选项分配了一个值,并检查其不是null
,之后for..in
循环遍历选项的键,然后您会看到target[name]
和{{的值1}}被分配给临时变量。
我基本上了解背景,但我仍然不理解下面的代码:
options[name]
有人可以解释上面这行是做什么的吗?我也不理解上面的评论
// Prevent never-ending loop
if (target === copy) {
continue;
}
P.S。 ::为简单起见,我们假设::
// Prevent never-ending loop.