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.
- "What semantic meaning (preferably related to real world objects) does
FunctionConstructor.prototypehave?" - If
let variable = new FunctionConstructor ();, then, on the one hand,variableinherits fromFunctionConstructor(since it was created using the operatornew FunctionConstructor ();), and on the other hand,variable .__ proto __ = FunctionConstructor.prototype(again because it was created with thenewoperator), i.e.variableprototypically inherits fromFunctionConstructor.prototype. What is the difference between prototypal inheritance and normal inheritance?
It is an object that was created at the time that
FunctionConstructorwas 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 aclassdefinition, ...).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 theclasssyntax, which results in similar initialisation of thatprototypeobject.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 bethisduring the function's execution and will be returned bynew. Thatthisobject 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.No, this is not true. The
variablewill not inherit anything fromFunctionConstructor.FunctionConstructoris an object (a function object) with its own prototype (which is not itsprototypeproperty, but its__proto__property), and that chain has nothing to do withvariableat all.It helps to visualise these two independent prototype chains. See How does JavaScript .prototype work?, where I elaborated on this in my answer.
This is true.
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
prototypeproperty. Prototypes are just objects, and can be modified, classes not.Read more: