Object.create()
Object.create() creates a new object whose prototype is the object supplied as its first argument. It can also define the new object’s own properties through an optional property-descriptor map.
`Object.create(proto)` returns a new object with `proto` as its prototype. The new object does not copy `proto`'s properties into itself; inherited property lookup reaches the prototype when needed.
The first argument can be an object or `null`. Using `null` creates an object with no prototype, so it does not inherit methods such as `toString` from `Object.prototype`.
An optional second argument defines own properties using property descriptors, similar to `Object.defineProperties()`. This is different from assigning properties after creation because descriptor attributes such as `writable`, `enumerable`, and `configurable` can be controlled explicitly.
`Object.create()` is closely related to JavaScript's prototype-based inheritance. Classes ultimately rely on prototype objects too, but `Object.create()` lets you construct an object with a chosen prototype directly without defining a class.
Key points
- The first argument becomes the new object's prototype.
- Properties on the prototype are inherited, not copied as own properties.
- `Object.create(null)` creates an object without `Object.prototype` in its prototype chain.
- The optional second argument defines own properties with property descriptors.
- Use `Object.getPrototypeOf()` to inspect the resulting object's prototype.
Interview questions
What does `Object.create()` do?
It creates a new object whose prototype is the object passed to `Object.create()`. Properties and methods on that prototype can therefore be accessed through the new object. Those inherited properties are not own properties of the new object. The optional second argument can define the new object's own properties using descriptors.
What is the difference between `Object.create(proto)` and `new Constructor()`?
`Object.create(proto)` creates an object with exactly the supplied prototype and does not call a constructor. `new Constructor()` creates an object whose prototype is normally `Constructor.prototype` and then invokes the constructor with the new object as its `this` value. Therefore, constructor initialization is a major behavioral difference. `Object.create()` is useful when you want direct prototype control without constructor execution.
Why would you use `Object.create(null)`?
It creates an object with no prototype at all. This means it does not inherit properties or methods from `Object.prototype`. Such objects can be useful as simple dictionaries when inherited keys or methods would be undesirable. However, they also lack familiar inherited methods such as `toString`, so code must account for that.
How does the optional second argument to `Object.create()` work?
The second argument is a property-descriptor object describing properties to define directly on the newly created object. Each descriptor can specify attributes such as `value`, `writable`, `enumerable`, and `configurable`, or use accessor fields such as `get` and `set`. This provides more control than ordinary assignment. The descriptors are applied to own properties of the newly created object.