-1

This is the code:

var master = [];
var obj = {};

obj.name = "A";
master.push(obj);
console.log(master);

obj.name = "B";
master.push(obj);
console.log(master);

Following is the output on console:

The name property should be A at one place and B at another place

enter image description here

But both properties are displayed as B only.

How to avoid this overriding?

Nemus
  • 3,879
  • 12
  • 38
  • 57
Sangram Jagtap
  • 79
  • 1
  • 2
  • 7

1 Answers1

0

That's what you wanted to do. First define a type, then instantiate 2 objects of it and at the end put them in an array and show the array with them and their value.

obj = function(name) {
  this.name = name;
}

var a = new obj("A");

var b = new obj("B");

var master = [a,b];

console.log(master);
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32