Object.getPrototypeOf(obj)
Returns the prototype of an object — the object that property lookups fall
back to when the own properties miss. This is the spec-sanctioned way to read
the internal [[Prototype]] slot.
Basic usage
const base = { greet() { return "hi"; } };
const child = Object.create(base);
child.name = "x";
Object.getPrototypeOf(child); // base
Object.getPrototypeOf(base); // Object.prototype
Object.getPrototypeOf(Object.prototype); // null (end of the chain)
Constructor prototypes
function Shape(name) { this.name = name; }
Shape.prototype.area = function () { return 0; };
const s = new Shape("tri");
Object.getPrototypeOf(s) === Shape.prototype; // true
Walking the whole chain
function chain(obj) {
const out = [];
for (let p = Object.getPrototypeOf(obj); p; p = Object.getPrototypeOf(p)) {
out.push(p);
}
return out;
}
chain(s); // [Shape.prototype, Object.prototype]
Setting a prototype
To replace the prototype after creation, use
Object.setPrototypeOf. It works but is slow in modern engines,
because it invalidates inline caches. Prefer building the chain at creation
time with Object.create:
const parent = { hello: 1 };
const o = Object.create(parent); // fast, idiomatic
o.hello; // 1
Why not __proto__?
obj.__proto__ does the same read, but it is a legacy accessor
from Annex B: it can be shadowed, it does not exist on
null-prototype objects, and it mixes reading with mutation.
Object.getPrototypeOf is defined on Object itself and
behaves uniformly, including for exotic objects:
const bare = Object.create(null);
bare.__proto__; // undefined (own missing property)
Object.getPrototypeOf(bare); // null
For class instances,
Object.getPrototypeOf(instance) === Class.prototype — a handy
assertion when debugging multiple-realm code (iframes, workers, VMs), where
instanceof can fail because each realm has its own copy of the
constructor.