本文解釋javascript中深拷貝和淺拷貝的區別。javascript
淺拷貝指拷貝了引用值。vue
var original = {"prop1" : "Prop1", "prop2" : "prop2"}; console.log(JSON.stringify(original)); // {"prop1" : "Prop1", "prop2" : "prop2"} var shallowCopy = original; console.log(JSON.stringify(shallowCopy)); // {"prop1" : "Prop1", "prop2" : "prop2"} shallowCopy.prop1 = "ChangedProp1"; console.log(JSON.stringify(original)); // {"prop1" : "ChangedProp1", "prop2" : "prop2"} console.log(JSON.stringify(shallowCopy)); // {"prop1" : "ChangedProp1", "prop2" : "prop2"}
https://smoothprogramming.com...
java
注意:jquery
深拷貝指遞歸的複製對象的屬性給新對象。jquery中咱們使用$.extend去進行深拷貝。數組
$.extend(deepCopy, target, object1, [objectN] )
第一個參數傳入true,指明此爲深拷貝,target爲目標對象,object1,原對象。函數
var original = {"prop1" : "Prop1", "prop2" : "prop2"}; console.log(JSON.stringify(original)); // {"prop1" : "Prop1", "prop2" : "prop2"} var deepCopy = $.extend(true, {}, original); console.log(JSON.stringify(deepCopy)); // {"prop1" : "Prop1", "prop2" : "prop2"} deepCopy.prop1 = "ChangedProp1"; console.log(JSON.stringify(original)); // {"prop1" : "Prop1", "prop2" : "prop2"} console.log(JSON.stringify(deepCopy)); // {"prop1" : "ChangedProp1", "prop2" : "prop2"}
https://smoothprogramming.com...
spa
注意:code
譯者注:對象