What Object.create Does
You'll learn how Object.create lets you create a new object with a specific prototype, giving you precise control over inheritance without needing constructor functions.
The Core Idea
Object.create() is a method that creates a brand new empty object and sets its [[Prototype]] (accessible via __proto__) to whatever object you specify. This gives you direct control over the prototype chain without involving constructor functions or the new keyword.
The syntax is straightforward:
const newObj = Object.create(prototypeObject);
The newObj will be a fresh, empty object whose [[Prototype]] points directly to prototypeObject.
Why This Matters
Before Object.create, you typically needed constructor functions to set up prototype relationships. Object.create offers a simpler, more direct approach when you just want to establish inheritance without the ceremony of constructors.
const animal = {
speak() {
return "Some sound";
}
};
// Create a new object with animal as its prototype
const dog = Object.create(animal);
dog.bark = function() {
return "Woof!";
};
console.log(dog.bark()); // "Woof!" (own property)
console.log(dog.speak()); // "Some sound" (inherited)
console.log(Object.getPrototypeOf(dog) === animal); // true
Creating Objects Without a Prototype
You can even create an object with no prototype by passing null:
const bare = Object.create(null);
console.log(Object.getPrototypeOf(bare)); // null
// bare has no inherited methods like toString()
Key Takeaway: Object.create(proto) creates a new object whose [[Prototype]] is set to proto, giving you manual, precise control over the prototype chain without constructor functions.