BonsaiDen / JavaScript-Garden

A collection of documentation about the most quirky parts of the JavaScript language.
http://bonsaiden.github.com/JavaScript-Garden
MIT License
3.45k stars 558 forks source link

Incorrect detail in section `#object.prototype` #364

Closed myst729 closed 4 years ago

myst729 commented 7 years ago

https://bonsaiden.github.io/JavaScript-Garden/#the-prototype-property

While the prototype property is used by the language to build the prototype chains, it is still possible to assign any given value to it. However, primitives will simply get ignored when assigned as a prototype.

function Foo() {}
Foo.prototype = 1; // no effect

It says Foo.prototype = 1 will get ignored and has no effect. But it's not.

function Bar(){}
(new Bar).constructor // function Bar(){}

function Foo(){}
Foo.prototype = 1
(new Foo).constructor // function Object() { [native code] }

(new Foo).constructor is not function Foo(){} anymore. So it's obviously affected by the assignment expression.

This is described in ECMA-262: 9.1.14 GetPrototypeFromConstructor ( constructor, intrinsicDefaultProto ), step 4 is the key point:

  1. Assert: intrinsicDefaultProto is a String value that is this specification's name of an intrinsic object. The corresponding object must be an intrinsic that is intended to be used as the [[Prototype]] value of an object.
  2. Assert: IsCallable(constructor) is true.
  3. Let proto be ? Get(constructor, "prototype").
  4. If Type(proto) is not Object, then
    1. Let realm be ? GetFunctionRealm(constructor).
    2. Let proto be realm's intrinsic object named intrinsicDefaultProto.
  5. Return proto.

So to the point, it's not no effect, nor ignored. Usually Object in implementations.

Credit goes to this post from StackOverflow.

myst729 commented 5 years ago

@peterjwest Hi, just came across and this was issued two years ago. Any plan to fix it?

myst729 commented 4 years ago

No response whatsoever.