FunctionConstructor.prototype

45 Views Asked by At

If

let variable = new FunctionConstructor();

then variable.__proto__ = FunctionConstructor.prototype. What is variable .__ proto__ (the object from which variable is inherited) is clear, but what is equal to the prototype property of a constructor function is not.

  1. "What semantic meaning (preferably related to real world objects) does FunctionConstructor.prototype have?"
  2. If let variable = new FunctionConstructor ();, then, on the one hand, variable inherits from FunctionConstructor (since it was created using the operator new FunctionConstructor ();), and on the other hand, variable .__ proto __ = FunctionConstructor.prototype (again because it was created with the new operator), i.e. variable prototypically inherits from FunctionConstructor.prototype. What is the difference between prototypal inheritance and normal inheritance?
1

There are 1 best solutions below

0
On

What semantic meaning [...] does FunctionConstructor.prototype have?

It is an object that was created at the time that FunctionConstructor was defined. So whenever a function is defined, it immediately gets an object property (prototype). Some exceptions exist where functions cannot be used as constructors (e.g. arrow functions, members of a class definition, ...).

This object can be given members like any other object. In old style code you'll often see FunctionConstructor.prototype.myMethod = ..... In modern versions you'll see the class syntax, which results in similar initialisation of that prototype object.

This object will serve as a prototype whenever the constructor is invoked with new. When such invocation takes place, even before any statement of that function is executed, a new object is created that will be this during the function's execution and will be returned by new. That this object will have its __proto__ property set to that prototype object, establishing the prototype chain. So this is the moment that the prototype starts to play its role.

If let variable = new FunctionConstructor ();, then, on the one hand, variable inherits from FunctionConstructor (since it was created using the operator new FunctionConstructor ();),

No, this is not true. The variable will not inherit anything from FunctionConstructor. FunctionConstructor is an object (a function object) with its own prototype (which is not its prototype property, but its __proto__ property), and that chain has nothing to do with variable at all.

It helps to visualise these two independent prototype chains. See How does JavaScript .prototype work?, where I elaborated on this in my answer.

...and on the other hand, variable.__ proto __ = FunctionConstructor.prototype (again because it was created with the new operator), i.e. variable prototypically inherits from FunctionConstructor.prototype.

This is true.

What is the difference between prototypal inheritance and normal inheritance?

There are many differences. Here are a few:

Classical inheritance relies on classes, which provide the template for instances. Classes define a hierarchy, but instances cannot inherit from each other. Prototypal inheritance relies on constructors and their prototype property. Prototypes are just objects, and can be modified, classes not.

Read more: