标签:reac func prope AC names mes foreach create def
function copy(obj) {
const copy = Object.create(Object.getPrototypeOf(obj));
const propNames = Object.getOwnPropertyNames(obj);
propNames.forEach(function(name) {
const desc = Object.getOwnPropertyDescriptor(obj, name);
Object.defineProperty(copy, name, desc);
});
return copy;
}
const obj1 = { a: 1, b: 2 };
const obj2 = copy(obj1); // obj2 looks like obj1 now
obj2.c=3;
console.log(obj2); //{ a:1,b:2,c:3 }
console.log(obj1); //{a: 1, b: 2 }
//对象之间会互相不影响
标签:reac func prope AC names mes foreach create def
原文地址:https://www.cnblogs.com/daiwenru/p/8882460.html