Null prototype, Object.prototype and Object.create

前端 未结 1 1003
余生分开走
余生分开走 2020-12-24 08:44

Why is it that setting the prototype property of a constructor function to null does not prevent objects created from that function from calling through to meth

相关标签:
1条回答
  • 2020-12-24 09:33

    In short

    Yes, your observation is correct - a function constructed with the new operator will always have an object prototype in this case Object.prototype and this is indeed unlike a function created with Object.create.


    On why

    One can see this behavior completely specified in the ES5 language specification on which JavaScript is based on. Let's see this.

    In new:

    Quoting the specification of the [[Construct]] method of functions that indicates how object creation using the new operator is performed we can see that the following is specified:

    If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype object as described in 15.2.4.

    In Object.create:

    On the other hand, if we check out The spec for Object.create we can see that Object.create(o) specifies:

    Set the [[Prototype]] internal property of obj to O.

    Which means we can set it, it also explicitly checks that it is null or Object in that algorithm (please do follow the link to the spec and read it :))

    So the prototype of the objects called with new Foo is Object.prototype and not null. It is impossible to create objects with no prototype without Object.create using standard methods only.

    0 讨论(0)
提交回复
热议问题