1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| function deepCopy(obj) {
if (Object.prototype.toString.call(obj) === '[object Array]') {
var len = obj.length, out = new Array(len), i = 0;
for ( ; i < len; i++ ) {
out[i] = arguments.callee(obj[i]);
}
return out;
}
if (typeof obj === 'object') {
var out = {}, i;
for ( i in obj ) {
out[i] = arguments.callee(obj[i]);
}
return out;
}
return obj;
}
var a = [[1], [2], [3]];
var b = deepCopy(a);
console.log(a); // [[1], [2], [3]]
console.log(b); // [[1], [2], [3]]
console.log(a === b); // false
b[0][0] = 99;
console.log(a); // [[1], [2], [3]]
console.log(b); // [[99], [2], [3]] |
Partager