A function’s prototype is the object instance that will become the prototype for all objects created using this function as a constructor
An object’s prototype is the object instance from which the object is inherited.
A prototype is not like a class, it is an object.
When a function is created, a prototype object is created and attached to it behind the scenes. If the function is then used as a constructor with the
new keyword, the object that is created has a
__proto__ property that is pointing to the same object that is the function’s prototype.
fluffy.__proto__ is pointing to the same instance as
Cat.prototype. To continue:
This also affects any new instance created:
This is a bit weird, especially when you consider the following.
fluffy has the property
"Purr", so the lookup stops early and we don’t get the default greeting of
You can change the prototype object on the constructor, although it can give some weird side effects:
The existing fluffy and muffin objects still have prototypes pointing to the original function prototype instance. This emphasises the prototypes are all instances of objects living in memory.
Down the rabbit hole.
What we haven’t seen yet is that the Cat prototype also has a prototype.
Object.create sets up the prototype chain and assigns Animal.prototype as the prototype for Cat.
There are some differences between these, the most important being that the
speak function is no longer enumerable.