Object.create()
Creates a new object with the given prototype and optional property descriptors.
Object.create(proto, propertiesObject)- Added in JavaScript ES5 (2009)
Parameters
| Parameter | Type | Description |
|---|---|---|
$proto | Object | null | The object which should be the prototype of the newly-created object. |
$propertiesObjectoptional | Object= undefined | If specified and not undefined, an object whose own enumerable properties specify property descriptors to be added to the new object. Corresponds to the second argument of Object.defineProperties(). |
Return value
ObjectA new object with the specified prototype object and properties.
Errors
Throws a TypeError if proto is neither null nor an object.
Object.create() makes a new object and wires its prototype in one step, instead of creating an object and reassigning its prototype afterwards. The second argument takes property descriptors, not plain values — the same shape Object.defineProperties() uses.
When to reach for it
The most valuable case is Object.create(null): an object with no prototype at all, which makes a genuinely safe dictionary. A plain {} already answers to toString, constructor and valueOf, so if (cache[key]) can return a truthy inherited function for a key nobody ever stored, and a key named __proto__ can rewrite the prototype instead of storing data. A null-prototype object has none of that surface.
It is also how you build an explicit prototype chain without classes, and how you clone an object's prototype linkage when subclassing manually.
What trips people up
Descriptors default to false: Object.create(proto, { x: { value: 1 } }) gives you a property that is non-writable, non-enumerable and non-configurable, so it will not show up in Object.keys() and assignments to it silently fail. Spell out writable and enumerable when you want normal behaviour. Null-prototype objects also have no hasOwnProperty — use Object.hasOwn().
Example
When to use it
- Build a lookup table or counter map that cannot collide with Object.prototype members
- Create an object that inherits from a specific prototype without using a class
- Make a prototype-less object to safely store keys that come from user input
- Define a new object with precise property attributes in a single call
Frequently asked questions
What does Object.create(null) do?
It creates an object with no prototype, so it inherits nothing from Object.prototype — the safest base for a dictionary keyed by untrusted strings, since keys like __proto__, toString or constructor cannot collide with inherited members.
Why is my Object.create() property missing from Object.keys()?
The second argument takes property descriptors, and enumerable defaults to false, so you must write { value: 1, enumerable: true, writable: true } to get a normal visible property.
What is the difference between Object.create() and a constructor or class?
Object.create() links a prototype directly with no constructor function involved and runs no initialisation code, whereas new Foo() creates the object, links Foo.prototype, and then runs the constructor body.
More examples
A safe counter map
Counting works even for the key "constructor"; with a plain {} the initial read would have returned the inherited Object constructor instead of undefined.
const words = ["a", "b", "a", "constructor", "a"];
const counts = Object.create(null);
for (const w of words) {
counts[w] = (counts[w] ?? 0) + 1;
}
console.log(Object.entries(counts));Descriptor flags default to false
The first object's x is invisible to Object.keys() because enumerable defaulted to false, while the fully specified one behaves like a normal property.
const strict = Object.create(Object.prototype, { x: { value: 1 } });
const normal = Object.create(Object.prototype, {
x: { value: 1, writable: true, enumerable: true, configurable: true },
});
console.log(Object.keys(strict), Object.keys(normal));
console.log(Object.getOwnPropertyDescriptor(strict, "x"));An explicit prototype chain
describe and kind are found up the chain while Object.keys() reports only Rex's own name, showing inheritance without any class syntax.
const animal = { describe() { return `${this.name} the ${this.kind}`; } };
const dog = Object.create(animal);
dog.kind = "dog";
const rex = Object.create(dog);
rex.name = "Rex";
console.log(rex.describe());
console.log(Object.keys(rex));
console.log(Object.getPrototypeOf(Object.getPrototypeOf(rex)) === animal);
Discussion