Course contentsShow
← JavaScript
Lesson 727 of 2,72317. Prototypes and InheritanceFree lesson

Implementing Inheritance with Object.create

Build inheritance hierarchies using Object.create to share behavior between parent and child objects.

Implementing Inheritance with Object.create

What you'll learn: How to use Object.create to build parent-child inheritance hierarchies where child objects share behavior from parent objects.

The Core Idea

Inheritance lets you create specialized objects that build upon more general ones. With Object.create, you can establish a prototype chain where a child object inherits properties and methods from a parent, allowing you to share common behavior while adding specialized functionality.

Think of it like a job hierarchy: all employees share basic behaviors (clocking in, taking breaks), but managers inherit those behaviors and add management-specific ones (approving requests, scheduling meetings).

Building an Inheritance Hierarchy

// Parent object with shared behavior
const animal = {
  alive: true,
  eat() {
    console.log(`${this.name} is eating`);
  }
};

// Child object inherits from animal
const dog = Object.create(animal);
dog.name = 'Buddy';
dog.bark = function() {
  console.log('Woof!');
};

dog.eat();   // "Buddy is eating" (inherited from animal)
dog.bark();  // "Woof!" (own method)
console.log(dog.alive);  // true (inherited property)

Multi-Level Inheritance

You can chain prototypes to create deeper hierarchies:

const mammal = Object.create(animal);
mammal.warmBlooded = true;

const cat = Object.create(mammal);
cat.name = 'Whiskers';
cat.meow = function() {
  console.log('Meow!');
};

console.log(cat.warmBlooded);  // true (from mammal)
console.log(cat.alive);        // true (from animal)

The lookup follows: cat → mammal → animal → Object.prototype → null.

Key Takeaway: Object.create lets you build inheritance hierarchies by setting up prototype chains, enabling child objects to inherit and extend behavior from parent objects while maintaining their own specialized properties and methods.