Object.create
Prototypal Inheritance
Learn how JavaScript objects can inherit properties from other objects using Object.create: the foundation of prototypal inheritance.
What is an Object in JavaScript?
An object is a collection of key-value pairs. Each key is called a property, and its value can be any data type: a string, number, array, function, or even another object.
The Prototype Chain
Every JavaScript object has a hidden link called [[Prototype]]. When you access a property that doesn't exist on an object, JavaScript follows the chain upward to the prototype, searching until it finds the property or reaches the end.
Why Object.create?
JavaScript provides several ways to create objects. Object.create is unique because it lets you explicitly set the prototype of a new object, giving you fine-grained control over inheritance.
The Syntax of Object.create
The Core Rule
How the Link Works
Object.create vs new Keyword
| Object.create | new Constructor() | |
|---|---|---|
| Mechanism | Sets prototype directly | Calls a constructor function |
| Constructor needed? | No | Yes |
| Runs initializer code? | No | Yes |
| Result | Empty object + prototype link | Object with own properties set |
| Use case | Pure inheritance, composition | Creating instances from a blueprint |
How to Apply It Step by Step
Example 1: Basic Inheritance
Example 2: Overriding Inherited Properties
Example 3: Multi-Level Chain
Special Case: Object.create(null)
Passing null as the prototype creates a truly empty object with no prototype at all. It won't inherit toString, hasOwnProperty, or any other built-in method.
Own vs Inherited Properties
Common Mistakes
Summary Comparison
| Method | What it does | Result |
|---|---|---|
| Object.create(proto) | Links new object's [[Prototype]] to proto | Empty object + inheritance |
| Object.assign(target, src) | Copies own enumerable properties from src to target | Flat copy, no prototype link |
| { ...spread } | Shallow copies own properties into a new literal | Flat copy, no prototype link |
| new Constructor() | Calls constructor, links to Constructor.prototype | Initialized instance |
Question 1 of 5
What does Object.create(proto) return?
Question 2 of 5
const base = { x: 10 };
const child = Object.create(base);
child.x = 20;
After running the code above, what is the value of base.x?
Question 3 of 5
What is the result of Object.create(null)?
Question 4 of 5
const proto = { greet() { return "hi"; } };
const obj = Object.create(proto);
Which expression correctly checks if greet is an own property of obj?
Question 5 of 5
How does Object.create differ from Object.assign?