Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Object.create: an explicit prototype for a new object

Object.create makes an ordinary object with exactly the specified prototype and optional own-property descriptors.

Version: ECMAScript 5 onward; example target: Node.js 24.22.2

Syntax

Object.create(proto, properties)

Object.create(proto, properties) requires proto to be an object or null; a number, string, or other primitive throws TypeError instead of being boxed. It returns a new ordinary object whose internal [[Prototype]] is proto. Passing null creates an object without Object.prototype, useful as a dictionary but without inherited methods.

Optional properties is processed like Object.defineProperties: enumerable own string and symbol keys map names to descriptor objects. Missing descriptor fields take restrictive defaults—writable, enumerable, and configurable default to false, while value, get, and set default to undefined. This is not ordinary value copying.

The method has existed since ECMAScript 5. A null properties argument, an invalid descriptor, or a getter/Proxy trap can throw. Inherited properties are read through the prototype chain but remain owned by the prototype, so changing that prototype can affect every created object. For external data, prefer an explicit key allowlist even with a null-prototype dictionary.

Examples

const proto = {
  role: "reader",
  describe() { return `${this.name}:${this.role}`; }
};
const user = Object.create(proto, {
  name: { value: "Ada", writable: true, enumerable: true, configurable: true },
  secret: { value: 7 }
});
console.log(Object.getPrototypeOf(user) === proto, user.describe());
console.log(Object.keys(user).join(","), Object.hasOwn(user, "role"));
const dictionary = Object.create(null);
dictionary.__proto__ = "data";
console.log(Object.getPrototypeOf(dictionary), dictionary.__proto__);
try { Object.create(42); } catch (error) { console.log(error.name); }

Result

true Ada:reader
name false
null data
TypeError

The example creates an object with a prototype and descriptors, plus a null-prototype dictionary.

Sources