Cloning an Object in Node.js

后端 未结 21 1926
情书的邮戳
情书的邮戳 2020-11-28 02:03

What is the best way to clone an object in node.js

e.g. I want to avoid the situation where:

var obj1 = {x: 5, y:5};
var obj2 = obj1;
obj2.x = 6;
con         


        
21条回答
  •  旧巷少年郎
    2020-11-28 02:23

    This code is also work cause The Object.create() method creates a new object with the specified prototype object and properties.

    var obj1 = {x:5, y:5};
    
    var obj2 = Object.create(obj1);
    
    obj2.x; //5
    obj2.x = 6;
    obj2.x; //6
    
    obj1.x; //5
    

提交回复
热议问题