Method to copy objects

  ①var obj1 = obj; obj heap which is assigned to the address of the referenced object obj1, such replication in a change zenaratame.

  ②assign () method: can replicate only be enumerated attribute (prototype chain properties should be noted here is not enumerated attribute), assgin () copying objects, the first layer is no reference to the relationship (if one of the objects to be changed, and deleted when, other affected), the back layer reference relationship. Syntax: var obj1 = Object.assgin ({} , obj); features: ① a plurality of objects can be copied into an object ② may traverse only copy attribute (object attribute is false enumerable property can not be copied, the prototype attributes can not be copied) ③ you can copy the frozen object properties
// var div = document.createElement ( "div ");

  ③ traversal replication, attributes of the source object may be an increase in the properties of the new object. This method for duplicating an object without reference relationship of the first layer, the back layer reference relationship. Syntax: for (var prop in obj) {obj1 [prop] = obj [prop]}

  ④ deconstruction assignment: this method will be the original object properties and methods delete, create new objects to cover the original object. The first layer is not a reference relationship, the back layer reference relationship. Syntax: var obj1 = {... obj};

  ⑤JSON copy: the original objects to a string, then converted object. This method is deep copy, i.e., the first layer and the rear layer are no reference relationship. This method will be the original object properties overwritten. Syntax: var obj1 = JSON.parse (JSON.stringify (obj));

Guess you like

Origin www.cnblogs.com/wuqilang/p/11204640.html