skip to Main Content

What functions’s [[Prototype]] points to?

In the below code, constructor function b‘s [[Prototype]] points to constructor function a.

class a{}
class b extends a{}

Object.getPrototypeOf(b)===a //true

2

Answers


  1. In the below code, constructor function b‘s [[Prototype]] points to constructor function a.

    Right, that’s how class syntax works. It sets up two parallel lines of inheritance:

    b −−−−−−−−−−−−−−> a −−−−−−−−−−−−−−> Function.prototype
    
    b.prototype −−−−> a.prototype −−−−> Object.prototype
    

    The prototype of function b is function a (which is how it inherits static methods). That’s covered by the specification’s abstract ClassDefinitionEvaluation operation (the function inheritance part is step 8(h)(iii) as of this writing) and the parts of the spec that call it.

    The prototype of an ordinary function is Function.prototype (see InstantiateOrdinaryFunctionObject). Generator functions, async functions, and async generator functions inherit from prototypes that aren’t directly exposed by a built-in property (1, 2, 3).

    Login or Signup to reply.
  2. The [[Prototype]] of class B is class A
    This is how static inheritance work – statics missing on class B are taken from its [[Prototype]], class A
    enter image description here

    The .prototype of class B is an instance of class A with assigned { constructor: class B }
    This is how instance inheritance works – properties missing on B.prototype are taken from its [[Prototype]], A { [[Prototype]]: A.prototype, constructor: class B }
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search