Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Object.create

Creates an object with the given prototype.

Available since: ES5

Syntax

Object.create(proto, properties)

Parameters

ParameterTypeDescription
protoobject | nullThe object that becomes the prototype of the new object.
propertiesobjectOptional. Property descriptions, as in Object.defineProperties.

Return value

objectA new object with the given prototype.

Examples

The object inherits the prototype's methods:

const animal = {
  speak() {
    return `${this.name} makes a sound`;
  },
};
 
const dog = Object.create(animal);
dog.name = 'Rex';
 
console.log(dog.speak());

Output:

Rex makes a sound

An object without a prototype — a clean dictionary:

const dict = Object.create(null);
dict.apple = 3;
 
console.log('toString' in dict);
console.log('toString' in {});

Output:

false
true

Notes

A prototype is an object from which another object borrows properties and methods it does not have itself. In everyday code classes set up prototypes, and Object.create() is rarely needed.

See also

In the tutorial

  • Prototypes — How inheritance works inside JavaScript: an object's prototype, the prototype chain, class methods in prototype and Object.create.

Thanks for reading!

Donate to the site — we work hard for you.