Search code examples
javascriptprototypeprototype-chain

Why don't constructor functions in Javascript have Function as their prototype?


I am learning Javascript and I recently learned a bit about prototypes. My understanding of prototypes is that they are literally objects in memory that each newly created object has a pointer to. When a new object is created, the constructor function used to create that object assigns the prototype of the newly created object to be the prototype of the constructor function. In other words, if I have a Circle constructor function, and I create a new Circle object, that new object and the Circle constructor function both point to the same reference.

My question is: if prototypes are supposed to refer to the "parent" object that created the object, such as a constructor function, why don't constructor functions, which are functions and thus objects, point to "Function" constructor as their prototype? If you try creating a new constructor like so:

function Circle() {}
let c1 = new Circle();

You'll see c1 has the following as its prototype which makes sense, since the Circle constructor created it

Circle {}
__proto__:
constructor: ƒ Circle()
__proto__: Object

and Circle.prototype is the same object as above. So why doesn't Circle.prototype point to Function as a prototype?


Solution

  • As you have observed, the following is true:

    Object.getPrototypeOf(c1) === Circle.prototype
    

    So it logically follows that, if we consider Circle to be an instance of Function, then:

    Object.getPrototypeOf(Circle) === Function.prototype
    

    You can confirm this below:

    // let Circle = new Function(); /* also works here */
    function Circle() {}
    let c1 = new Circle();
    
    console.log(Object.getPrototypeOf(c1) === Circle.prototype);
    console.log(Object.getPrototypeOf(Circle) === Function.prototype);

    The Object.getPrototypeOf() method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.

    This is not to be confused with Circle.prototype. You can read more about the distinction between these in MDN's article on Object prototypes, or feel free to browse any of the answers to __proto__ VS. prototype in JavaScript.

    Note that Circle.__proto__ and Object.getPrototypeOf(Circle) are equivalent, but __proto__ is non-standard (even though it happens to be defined in almost every implementation of JavaScript due to its historical usage for backwards compatibility).