creation
Object.create
Creates a new object with the specified prototype and optional property descriptors. Pass null to create an object with no prototype at all.
Object.create(proto, propsObject?) Parameters
| Parameter | Purpose |
|---|---|
| proto | The prototype for the new object, or null |
| propsObject | Optional map of property names to descriptors (like defineProperties) |
| returns | A new object |
Examples
const proto = { greet() { return 'hi'; } };
const obj = Object.create(proto);
console.log(obj.greet()); Logs 'hi' — greet is inherited
const bare = Object.create(null);
console.log(bare.toString); Logs undefined — no Object.prototype methods
const o = Object.create({}, { x: { value: 1, enumerable: true } });
console.log(o.x); Logs 1
const clone = Object.create(Object.getPrototypeOf(orig = { a: 1 }), Object.getOwnPropertyDescriptors(orig));
console.log(clone.a); Logs 1 — creates a shallow clone preserving prototype and descriptors
Gotcha
The second argument uses property DESCRIPTORS (like defineProperties), not plain values — properties default to non-enumerable, non-writable, non-configurable if not specified. Object.create(null) creates a 'dictionary' object safe for arbitrary keys.
Related methods
Object.getPrototypeOf
Returns the prototype (i.e. the internal [[Prototype]]) of the specified object, or null if it has none. Preferred over the legacy __proto__ accessor.
Object.setPrototypeOf
Sets the prototype (i.e. the [[Prototype]]) of a specified object to another object or null. Considered a slow operation and generally discouraged — prefer Object.create at construction time.
Object.defineProperties
Defines multiple new or modified properties on an object by supplying a descriptors map. The batch-form counterpart of Object.defineProperty.
Object.fromEntries
Transforms a list of key-value pairs into an object — the inverse of Object.entries. Introduced in ES2019 and accepts any iterable of [key, value] pairs, including Map.
Object.assign
Copies all enumerable own properties from one or more source objects to a target object, then returns the modified target. Performs a shallow merge, invoking source getters and target setters.