Object.create vs class syntax
Object.create() vs class syntax in JavaScript
When you're just starting to learn JavaScript, you usually create objects by simply writing {}. But when you start trying to build smarter things—for example, reusable objects with some common behavior—you need to decide how to create those objects.
This is where two well-known approaches come into play:
Both are used, and both are related to the prototype system. But there are differences in how they work and what they're best suited for.
const animal = { speak() { console.log(`I'm a ${this.type}`); } }; const cat = Object.create(animal); cat.type = "cat"; cat.speak(); // I'm a cat
But what's happening here?
cat
, whose "parent" (prototype) is animal
.speak
method isn't found in cat
, JavaScript will look up to animal
and find it there.This is called the prototype chain.
class Animal { constructor(type) { this.type = type; } speak() { console.log(`I'm a ${this.type}`); } } const cat = new Animal("cat"); cat.speak(); // I'm a cat
What's the difference?
It creates the same thing but in a different way. class
is syntactic sugar—under the hood, it still works with the same prototype mechanism.
constructor
, super
, or extends
When using Object.create()
, you can immediately specify that a property:
writable: false
)enumerable: false
)const user = Object.create({}, { name: { value: "Anna", writable: false, enumerable: true } }); user.name = "Lilit"; console.log(user.name); // "Anna"
Doing something similar with class
is more complicated.
You'd have to use Object.defineProperty
inside the constructor, which makes the code more complex and harder to understand.
class User { constructor(name) { Object.defineProperty(this, 'name', { value: name, writable: false, enumerable: true }); } }
Both of these approaches create prototype-based inheritance. But class
is a newer and more "feature-rich" syntax, while Object.create()
is lower-level and more flexible.
class Person { constructor(name) { this.name = name; } } // This is the same as this code: function Person(name) { this.name = name; } Person.prototype.greet = function () { return `Hello, I'm ${this.name}`; };