Я прохожу учебник о том, как делать точные копии свойств, которые являются объектами и массивами.Этот процесс называется глубоким копированием.Вот код, который будет делать именно это:
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 ? [] : {};