JS does not seek properties in __proto__ if the facility is created by Object.create(null)



  • Here's the code:

    var o1 = Object.create(null);
    o1.__proto__ = { x: 1 };
    alert(o1.x); // undefined
    

    var o2 = {};
    o2.proto = { x: 1 };
    alert(o2.x); // 1

    var o3 = Object.create(null);
    Object.setPrototypeOf(o3, { x: 1 })
    alert(o3.x); // 1

    var tmp = Object.create(null);
    tmp.x = 1;
    var o4 = Object.create(null);
    Object.setPrototypeOf(o4, tmp);
    alert(o4.x); // 1
    // console.dir(o4) -> No Properties

    Why? o1.x != 1?



  • I'll turn the next one around:

    If you look at the specification, you see the description. http://www.ecma-international.org/ecma-262/6.0/#sec-object.prototype.__proto__

    It's a couple of methods. http://www.ecma-international.org/ecma-262/6.0/#sec-get-object.prototype.__proto__ ♪ http://www.ecma-international.org/ecma-262/6.0/#sec-set-object.prototype.__proto__ functions http://www.ecma-international.org/ecma-262/6.0/#sec-object.getprototypeof ♪ http://www.ecma-international.org/ecma-262/6.0/#sec-object.setprototypeof

    It is particularly worth noting that this is in fact

    Object.prototype.__proto__
    

    I mean, this property is described in Prototype Object

    In the construction of the facility Object.create(null) There is no such characteristic in the prototype of the facility that has been created and is assigned only as a normal field.

    If you're interested in the details of prototypes in ES6, you can read it. http://www.2ality.com/2015/09/proto-es6.html ♪


    http://www.ecma-international.org/ecma-262/6.0/#sec-additional-ecmascript-features-for-web-browsers

    The addition to the specification in which the characteristic is described indicates that everything in it is necessarily implemented if the code is performed in the browser. But in other systems, the designs described may not be available.

    In addition, the supplement describes the characteristics and characteristics of the ECMAScript in browsers inherited from the reality. The language characteristics and behaviour described have one or more undesirable characteristics and will be removed from the specification as far as possible. However, the use of a vast number of web pages has forced the browsers to continue to support them.

    http://www.ecma-international.org/ecma-262/6.0/#sec-additional-ecmascript-features-for-web-browsers




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2