Consider the Person() constructor used to create the tom object. We copy the tom object to a new variable called newTom. Consequently, newTom is also a object, same as tom.

 

Now, any modification to any property or method of the new object, newTom will also reflect in the old object tom. See the example below.

 

function Person(name,age){
  this.name = name;
  this.age = age;
  this.describe = function(){
    return "The name of the person is "+this.name+" and his/her age is "+this.age
  }
}

var tom = new Person("TomTom", 24);
var newTom = tom;
newTom.name = "NewTomTom"
console.log(newTom.name); //returnns "NewTomTom" 
console.log(tom.name); //this also return "newTomTom" 

 

This means that. when you copy object to a new variable, the variable get a reference of the original object. Therefore, we know that, object remains one, even when copied. Any modification to the properties/methods of the copied object will result in the same changes in the original object.

 


If the previous example was too heavy to sink in, let us take another example, this time with object literal notation.

 

var Person = {
  name:"Rishi",
  work:"Blogging"
}

var Workman = Person;

Workman.work = "Programming";

console.log(Person.work); //returns "Programming"

 

So you saw that how change in the work property of Workman object resulted in the same change in Person object. As mentioned earlier, this happens because Workman object has a reference to Person object.

 

0 Do You Love This Article ?

CT

Hello All! You are reading the author(s) profile of CorvineLabs Team. We may be 1,2 or ..any in number. Let that remain withing us. You enjoy our write up. If you want to join the team than do Contact Us.

Leave a Reply

Your email address will not be published. Required fields are marked *