What is Object.create()?

Object.create(proto, descriptors?) creates a new, empty object whose prototype is set to proto, letting you set up inheritance directly without constructors or classes.

What it is used for

  • Prototype delegation: share behaviour by linking objects (const admin = Object.create(user)).
  • Pure dictionaries: Object.create(null) gives an object with no prototype, so keys like "constructor", "toString" or "__proto__" are just data and cannot collide with inherited properties.
  • Legacy inheritance: Child.prototype = Object.create(Parent.prototype) was how constructor functions inherited before class.
  • Defining properties with descriptors: the optional second argument works like Object.defineProperties.

Property descriptors

Each property can be configured with value, writable, enumerable and configurable (or get/set). Properties defined through descriptors default to false for all three flags, which surprises people: they are read-only and hidden from Object.keys unless you say otherwise.

Object.create vs new

new Fn() creates an object linked to Fn.prototype and runs Fn. Object.create(Fn.prototype) only does the linking; no constructor runs.

Today

For dictionaries, a Map is often clearer. For inheritance, class is more readable. Object.create remains the precise tool when you want explicit control over the prototype.

Code example

js
1const userMethods = {
2 greet() {
3 return `Hi, I'm ${this.name}`;
4 },
5 isAdmin() {
6 return false;
7 },
8};
9
10const ada = Object.create(userMethods);
11ada.name = "Ada";
12console.log(ada.greet(), Object.keys(ada)); // Hi, I'm Ada ["name"]
13
14// Specialise by linking another level
15const adminMethods = Object.create(userMethods);
16adminMethods.isAdmin = () => true;
17const root = Object.create(adminMethods, {
18 name: { value: "root", enumerable: true }, // writable defaults to false!
19});
20console.log(root.greet(), root.isAdmin()); // Hi, I'm root true
21root.name = "changed"; // ignored (throws in strict mode)
22console.log(root.name); // root
23
24// A safe dictionary with no inherited keys
25const counts = Object.create(null);
26for (const word of ["constructor", "toString", "constructor"]) {
27 counts[word] = (counts[word] ?? 0) + 1;
28}
29console.log(counts.constructor, counts.toString); // 2 1 - just data
30const plain = {};
31console.log(typeof plain.constructor); // "function" - inherited from Object.prototype

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.

Open in study app