Object.getPrototypeOf()
The standard replacement for the __proto__ accessor. Its companion setPrototypeOf exists too, and every engine documentation entry for it amounts to a warning not to call it.
Demo
A prototype object cannot be printed usefully, so the demo reports which well-known prototype was found. A plain object gives [true, false] — its prototype is Object.prototype. An array gives [false, true], because its prototype is Array.prototype, which is where every array method lives. Array.prototype in turn has Object.prototype above it, which is how an array ends up with both push and hasOwnProperty. That two-step chain is exactly what instanceof walks.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| object | object | yes | The object to inspect. Primitives are coerced to their wrapper, so a number yields Number.prototype. null and undefined throw TypeError. |
Return value
object | null — The object prototype — one link up the chain. null for an object created with Object.create(null), and for Object.prototype itself.
Common patterns
Object.assign(Object.create(Object.getPrototypeOf(src)), src);
const isPlain = o != null && (Object.getPrototypeOf(o) === Object.prototype || Object.getPrototypeOf(o) === null);
const o = Object.create(proto);
Examples
Pitfalls
const o = {}; Object.setPrototypeOf(o, proto);
const o = Object.create(proto);
let p = o; while (true) p = Object.getPrototypeOf(p);
let p = o; while ((p = Object.getPrototypeOf(p)) !== null) { }
Object.getPrototypeOf([]) === Object.prototype
Object.prototype.isPrototypeOf([])
const o = {}; o.__proto__ = {polluted: 1}; Object.getPrototypeOf(o).polluted
const d = Object.create(null); d.__proto__ = {x: 1};
When to use
- Copying an object while preserving its prototype
- Distinguishing a plain object from a class instance
- Introspection and debugging of a prototype chain
- Reaching a prototype to list its methods
- Changing a prototype after creation → Object.create, or a class
- Checking a chain relationship → instanceof or isPrototypeOf
- Reading __proto__ → this is its standard replacement
- Building a dictionary → Object.create(null) or a Map
Notes
FAQ
getPrototypeOf. The __proto__ accessor is Annex B — normatively optional, kept only for web compatibility — and it is the vector for prototype pollution when it appears as a key in untrusted data. The static method has neither problem.
Object.getPrototypeOf(o); // standard o.__proto__; // legacy