Arrays as attributes in literal objects

It’s not easy for me to explain the problem, but just look at the sample code.

var test = { 
	my_array: [],
	my_var: ''
}


var a = Object.create(test);
var b = Object.create(test);


a.my_array.push('aaa');
b.my_array.push('bbb');

a.my_var = 'this is obj A';
b.my_var = 'this is obj B';


document.write(a.my_array[0]); //output: aaa
document.write('<br>');
document.write(b.my_array[0]); //output: aaa
document.write('<br>');
document.write(a.my_var); //output: this is obj A
document.write('<br>');
document.write(b.my_var); //output: this is obj B
Run codeHide result

How is it possible that object b has the same array value as Object a ?

+4
source share
5 answers

You have created two objects that exchange one prototype of the object. Properties of prototype objects are detected when searching for objects; they are not copied to instances of objects at creation.

When assigning a property value to an object, as in

a.my_var = 'this is obj A';

"" , . .

+2

, my_array . .

var test = function () {
    return { 
	  my_array: [],
	  my_var: ''
    }
}

var a = Object.create(test());
var b = Object.create(test());

a.my_array.push('aaa');
b.my_array.push('bbb');

a.my_var = 'this is obj A';
b.my_var = 'this is obj B';

document.write(a.my_array[0]); //output: aaa
document.write('<br>');
document.write(b.my_array[0]); //output: aaa
document.write('<br>');
document.write(a.my_var); //output: this is obj A
document.write('<br>');
document.write(b.my_var); //output: this is obj B
Hide result
+1

, :

my_array: []

Object.create , //. .

0

, :

The objects

( console.log(a, b))

:

(https://jsfiddle.net/dafuaLkj/):

var test = function() {
  this.my_array = [];
    this.my_var = ''
} 


var a = new test();
var b = new test();
0

, , - . , . . , Object.create() , .

( , ), . . (: MDN)

function clone(objectToBeCloned) {
  // Basis.
  if (!(objectToBeCloned instanceof Object)) {
    return objectToBeCloned;
  }

  var objectClone;
  
  // Filter out special objects.
  var Constructor = objectToBeCloned.constructor;
  switch (Constructor) {
    // Implement other special objects here.
    case RegExp:
      objectClone = new Constructor(objectToBeCloned);
      break;
    case Date:
      objectClone = new Constructor(objectToBeCloned.getTime());
      break;
    default:
      objectClone = new Constructor();
  }
  
  // Clone each property.
  for (var prop in objectToBeCloned) {
    objectClone[prop] = clone(objectToBeCloned[prop]);
  }
  
  return objectClone;
}

var test = { 
	my_array: [],
	my_var: ''
}


var a = clone(test);
var b = clone(test);


a.my_array.push('aaa');
b.my_array.push('bbb');

a.my_var = 'this is obj A';
b.my_var = 'this is obj B';


document.write(a.my_array[0]); //output: aaa
document.write('<br>');
document.write(b.my_array[0]); //output: aaa
document.write('<br>');
document.write(a.my_var); //output: this is obj A
document.write('<br>');
document.write(b.my_var); //output: this is obj B
Hide result
0

Source: https://habr.com/ru/post/1662550/


All Articles