Assign value without reference in javascript

I am having a slight problem with assigning objects in javascript.

take a look at this sample code that reproduces my problem.

var fruit = { name: "Apple" }; var vegetable = fruit; vegetable.name = "potatoe"; console.log(fruit); 

he writes

 Object {name: "potatoe"} 

How can I assign a value to not an object reference to another object?

+7
javascript variable-assignment
source share
1 answer

You can use Object.assign :

 var fruit = { name: "Apple" }; var vegetable = Object.assign({}, fruit); vegetable.name = "potatoe"; console.log(fruit); 
+15
source share

All Articles