ReviseAlgo Logo

Objects & Prototypes

Object.create & Object.getPrototypeOf

Master direct prototype management in JavaScript. Learn to construct objects with custom prototypes using Object.create and inspect linkages using Object.getPrototypeOf.

Last Updated: July 15, 2026 10 min read

1. Introduction

Managing prototype linkages directly is essential for advanced object behaviors. JavaScript provides static methods, Object.create (for creating objects with a custom prototype) and Object.getPrototypeOf (for inspecting an object's prototype link), to manage prototypes cleanly.

2. Why It Matters

Using these static methods avoids the performance penalties associated with mutating prototypes via the legacy __proto__ setter or Object.setPrototypeOf. They also let you create clean dictionary objects that inherit no properties or methods.

3. Real-World Analogy

Think of Renting a Pre-Furnished Apartment:

  • Object.create (Renting a Furnished Apartment): You move into an apartment. You don't buy the furniture (own properties); it is already there for you to use (inherited from the prototype). If you buy your own chair, you use it instead of the landlord's chair (property shadowing).
  • Object.create(null) (Renting a Bare Room): A completely empty room with no walls, outlets, or utilities. You must bring in everything you need yourself. There are no pre-existing items to inherit.
  • Object.getPrototypeOf (Inspecting Building Blueprints): Asking the building manager to see the blueprints to confirm who owns the utilities in your apartment (verifying the prototype source).

4. Prototype Management API

Let's explore the usage of these methods:

1. Object.create(proto, propertiesObject):

Creates a new object, setting its internal [[Prototype]] link to the first argument proto.

2. Creating Clean Dictionary Objects:

Passing null to Object.create creates an object that inherits nothing. It has no prototype link, meaning it lacks default properties like toString or valueOf.

3. Object.getPrototypeOf(obj):

Returns the prototype of the specified object. This is the standard, safe alternative to the deprecated __proto__ getter.

5. Practical Example

This script demonstrates setting up prototype-based inheritance between parent and child constructors using Object.create:

6. Common Mistakes

  • Mutating prototypes at runtime using Object.setPrototypeOf: Modifying the prototype of an already instantiated object is extremely slow in all modern JavaScript engines. It ruins optimization paths, affecting not just the mutated object but any code accessing its properties. Always use Object.create during creation instead.

7. Quick Quiz

Q1: Which method should you use in production to safely inspect the prototype of an object instance?

A) obj.__proto__

B) Object.getPrototypeOf(obj)

Answer: B — Object.getPrototypeOf(obj) is the standard, safe API recommended for inspecting prototypes in production.

8. Scenario-Based Challenge

The Clean Configuration Registry:

You are designing a secure plugin config storage system. External scripts can register configs as keys. Explain why using Object.create(null) is safer than using a plain object {} to prevent prototype pollution attacks (where malicious scripts overwrite methods like hasOwnProperty).

9. Debugging Exercise

Explain why this constructor inherits duplicate values, and how to fix it:

function Parent() {
  this.values = [1, 2];
}
function Child() {}

// Attempting inheritance Child.prototype = new Parent(); // bad inheritance practice! const c1 = new Child(); const c2 = new Child(); c1.values.push(3); console.log(c2.values); // logs [1, 2, 3]! State is shared!

View Solution

Diagnosis: Using new Parent() to set the child prototype causes instance properties (like this.values) to be placed on the shared prototype object. This means all Child instances share the exact same array reference, leading to state mutations across instances.

Fix: Use Object.create() to link the prototypes, and call the parent constructor inside the child constructor to initialize instance properties separately:

function Child() {
  Parent.call(this); // Inherit instance properties separately
}
Child.prototype = Object.create(Parent.prototype); // Inherit prototype methods only
Child.prototype.constructor = Child;

11. Interview Questions

🟢 Q1: Why is Object.create(null) useful for implementing dictionary objects?

Answer: Object.create(null) creates an object that has no prototype link, meaning it inherits no properties or methods from Object.prototype. This makes it ideal for dictionary objects because key lookups are protected from accidental matches with inherited methods (like toString, valueOf, or hasOwnProperty). It also prevents prototype pollution attacks, where malicious scripts attempt to overwrite default prototype properties.

12. Production Considerations

  • Prototype Pollution Prevention: In production web APIs parsing third-party JSON configurations, use Object.create(null) as the base dictionary, or validate inputs strictly to prevent prototype pollution attacks that exploit inherited properties.