SkillByAIOpen interactive version →

Lesson 31 / 40

Prototypes

Understand prototype chains and Object.create, the mechanism behind JavaScript inheritance.

The prototype chain

Every object has an internal link to a prototype object it falls back to for properties or methods it doesn't have directly.

Prototype in action

Object.create builds an object whose prototype is explicitly set to another object.

const animal = { eats: true };
const rabbit = Object.create(animal);
console.log(rabbit.eats);        // true, inherited
console.log(rabbit.hasOwnProperty("eats"));  // false

A chain of fallbacks

Think of the prototype chain like asking a parent, then grandparent for something you don't have — JS keeps climbing until it finds the property or hits null.