www.w3cschool.cn/article/8b3…
- Shallow copy
- There will still be references to objects
function copy(obj){
var objCopy = {};
for(var key in obj){
objCopy[key] = obj[key];
}
return objCopy;
}
var person = {name: "Jason".age: 18.car: {brand: "Ferrari".type: "430"}};
var personCopy = copy(person);
Copy the code
- Deep copy
- There are only Number, String, Boolean, Array types
- If you need other types, you can do the conversion yourself
function jsonClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
var clone = jsonClone({ a:1 });
Copy the code