Javascript - Effect of assigning empty object -


javascript learner - please understand ,

in below js snippet, when "nickname" property added object person1, reflects in person object well.

but, when person1 set empty object {} , please understand why not affect person object?


var person  = {   "firstname" : "tony",   "lastname" : "stark" };  var person1 = person;  person1.nickname = "ironman";  console.log(person);  //object { firstname: "tony", lastname: "stark", nickname: "ironman" } console.log(person1); //object { firstname: "tony", lastname: "stark", nickname: "ironman" }    person1 = {};  console.log(person); //object { firstname: "tony", lastname: "stark", nickname: "ironman" }   console.log(person1); //object {  } 

thanks in advance!!

in below js snippet, when "nickname" property added object person1, reflects in person object well.

you person1 = person so, both variables point same memory address varying 1 reflect in other too.

but, when person1 set empty object {} , please understand why not affect person object?

by doing person1 = {}, you're assigning new memory address person , person1 2 different entities , changing 1 not affect other.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -