Skip to main content

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:

javascript
Object.create(proto, [propertiesObject])
ParameterDescription
protoThe 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

javascript
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:

  • user is the prototype ([[Prototype]]) for tim;
  • tim has no own sayHi method, but it is inherited via the prototype.

Example 2 - checking the prototype

javascript
console.log(Object.getPrototypeOf(tim) === user); // true

Object.create() literally creates a new object whose __proto__ equals user.


Example 3 - an object with no prototype

javascript
const data = Object.create(null); data.a = 1; console.log(data); // { a: 1 } console.log(Object.getPrototypeOf(data)); // null console.log(data.toString); // undefined

Such 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():

javascript
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"

javascript
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!" - own

A great way to implement simple prototypal inheritance without classes and constructors.


Example 6 - an alternative to class

javascript
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 doesExampleResult
Creates an object with the given prototypeObject.create(proto)A new object whose [[Prototype]] = proto
Creates an object with no prototypeObject.create(null)A "clean" object with no toString, hasOwnProperty
Properties with attributes can be setObject.create({}, { key: { value: 1 } })Equivalent to defineProperty()
Used for inheritanceconst child = Object.create(parent)child inherits parent's properties

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.