Object.create
Creates an object with the given prototype.
Available since: ES5
Syntax
Object.create(proto, properties)Parameters
| Parameter | Type | Description |
|---|---|---|
| proto | object | null | The object that becomes the prototype of the new object. |
| properties | object | Optional. 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 soundAn 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
trueNotes
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
- Object.getPrototypeOf — Returns the prototype of an object.
- class — Declares a class — a template for objects with shared methods.
- Object.defineProperty — Defines a property with exact settings: read-only, hidden, with a getter.
In the tutorial
- Prototypes — How inheritance works inside JavaScript: an object's prototype, the prototype chain, class methods in prototype and Object.create.