我正在阅读有关如何制作对象和数组属性的真实副本的教程。此过程称为深层复制。这是将执行此操作的代码:
function deepExtend(source, destination) {
for (var s in source) {
if (source.hasOwnProperty(s)) {
if (typeof source[s] === "object") {
destination[s] = isArray ? [] : {};
deepExtend(source[s],destination[s]);
} else {
destination[s] = source[s];
}
}
}
function isArray(o) {
return (typeof o === "[object Array]");
}
}
var person = {
array1: [1,2,4,5],
name: "Karen",
address: {
street: "1 Main St",
city: "Baltimore"
},
scores: [212, 310, 89],
say: function () {
console.log(this.name + ", " + this.address.street + ", " +
this.address.city + ", " + this.scores );
}
};
var employee = { salary: "$45,000" };
deepExtend(person, employee);
employee.say(); // => Karen, 1 Main St, Baltimore, 212, 310, 89
在三元运算部分,如何将参数传递给isArray函数?
destination[s] = isArray ? [] : {};