What does Object.create() do?
Object.create() is one of the basic methods for working with prototypes in JavaScript.
It lets you create a new object by manually specifying its prototype (what it inherits from).
In short
Syntax:
Object.create(proto, [propertiesObject])| Parameter | Description |
|---|---|
proto | The object that becomes the prototype of the new object (or null) |
propertiesObject (optional) | Extra properties for the new object (in descriptor format) |
Example 1 - basic usage
const user = {
sayHi() {
console.log(`Hello, ${this.name}!`);
}
};
const tim = Object.create(user); // create an object that inherits from user
tim.name = 'Tim';
tim.sayHi(); // "Hello, Tim!"Here:
useris the prototype ([[Prototype]]) fortim;timhas no ownsayHimethod, but it is inherited via the prototype.
Example 2 - checking the prototype
console.log(Object.getPrototypeOf(tim) === user); // trueObject.create() literally creates a new object whose __proto__ equals user.
Example 3 - an object with no prototype
const data = Object.create(null);
data.a = 1;
console.log(data); // { a: 1 }
console.log(Object.getPrototypeOf(data)); // null
console.log(data.toString); // undefinedSuch an object inherits nothing, not even toString, hasOwnProperty, etc.
It is used for plain dictionaries (map objects) with no name conflicts.
Example 4 - with propertiesObject (the second argument)
You can set properties with configuration right away, as in Object.defineProperty():
const person = Object.create({}, {
name: { value: 'Tim', writable: false, enumerable: true },
age: { value: 25, writable: true }
});
console.log(person.name); // "Tim"
person.name = 'Oleh'; // will not change
console.log(person.name); // "Tim"The second parameter lets you describe property attributes in detail
(value, writable, enumerable, configurable).
Example 5 - manual "inheritance"
const Animal = {
eat() {
console.log('I eat');
}
};
const Dog = Object.create(Animal);
Dog.bark = function() {
console.log('Woof!');
};
Dog.eat(); // "I eat" - inherited from Animal
Dog.bark(); // "Woof!" - ownA great way to implement simple prototypal inheritance without classes and constructors.
Example 6 - an alternative to class
const UserProto = {
init(name) {
this.name = name;
return this;
},
greet() {
console.log(`Hello, ${this.name}`);
}
};
const user = Object.create(UserProto).init('Tim');
user.greet(); // "Hello, Tim"This approach is often used in a functional OOP style - no classes, but inheritance via the prototype.
Summary
| What it does | Example | Result |
|---|---|---|
| Creates an object with the given prototype | Object.create(proto) | A new object whose [[Prototype]] = proto |
| Creates an object with no prototype | Object.create(null) | A "clean" object with no toString, hasOwnProperty |
| Properties with attributes can be set | Object.create({}, { key: { value: 1 } }) | Equivalent to defineProperty() |
| Used for inheritance | const child = Object.create(parent) | child inherits parent's properties |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.