JavaScript Prototypes & Inheritance
In JavaScript, objects have a special hidden property `[[Prototype]]` (historically accessed via `__proto__`), that is either `null` or references another object. That object is called "a prototype".
The Prototype Chain
When you read a property from a JavaScript object, and it's missing, JavaScript automatically takes it from the prototype. If it can't find it there, it looks at the prototype's prototype, and so on, until it hits `null`. This is known as "prototypal inheritance".
The F.prototype Property
Constructors (functions used with `new`) have a special property named `prototype`. When an object is created via a constructor, the new object's `__proto__` is set to the value of the constructor's `prototype` property. This allows all instances to share methods.
Object.create() vs Classes
While modern JavaScript provides the `class` syntax, under the hood it is still just syntax sugar over prototypes. `Object.create(proto)` allows you to directly specify an object's prototype without dealing with constructor functions, leading to "pure" prototypal inheritance (sometimes called OLOO - Objects Linking to Other Objects).
View Full Transcript+
This section contains the full detailed transcript of the interactive journey. It explains how JavaScript avoids duplicating methods for every object instance by storing them once in a prototype object, and connecting instances via the hidden internal [[Prototype]] link. It also explains the difference between the 'prototype' property on functions and the '__proto__' accessors on instances.
