?> 此题也就是深拷贝,就像jQuery
的extend
方法
var obj = {
a: 1,
b: 2,
c: {
d: 3
}
}
# 浅拷贝
var c1 = $.extend({}, obj);
console.log(c1.a === obj.a); // true
//深拷贝
var c2 = $.extend(true, {}, obj);
console.log(c2.a === obj.a); //false
var obj = {a: 1 ,b: 2}
var objclone = Object.assign({},obj);// 指向不同的对象到对象
let obj = {
a: 1,
b: 2,
c: {
age: 30
}
};
var objclone = Object.assign({},obj);
console.log('objclone: ', objclone);
obj.c.age = 45;
console.log('After Change - obj: ', obj); // 45 - This also changes
console.log('After Change - objclone: ', objclone); // 45
参考资料: