I am trying to implement a prototype object in this way:

var x1 = { self:false, max:true } var x2 = { self:true } x1.__proto__ = x2; console.log(x2.max); 

I end up with an undefined error.

When I use this method, everything works:

  var x1 = { self:false, max:true } var x2 = { self:true } x2.__proto__ = x1; console.log(x2.max); 

What is the difference?

  • the difference in the penultimate line of code. - aleksandr barakin
  • I understand that if the fence is not green, then it has a different color, but it does not speak about the properties of its paint and how it works - modelfak
  • You may already be up to date, but I want to warn you that it should be used with caution. You can read here: Object.prototype .__ proto__ - Aleksander K.
  • Yes, some IE browsers do not support it - modelfak

1 answer 1

If one object has a special reference __proto__ to another object, then when reading a property from it, if the property is absent in the object itself, it is searched for in the __proto__ object.

 x1.__proto__ = x2; // x1 наследует x2, в x2 нет свойств x1