Object.getPrototypeOf()
ES5+Returns the prototype of the specified object.
Syntax
Object.getPrototypeOf(obj)Parameters
obj Object The object whose prototype is to be returned
Return Value
The prototype of the given object
Examples
const arr = [1, 2, 3];
console.log(Object.getPrototypeOf(arr) === Array.prototype); 📌 When to Use
Use Object.getPrototypeOf() when you need to inspect the prototype chain, verify inheritance relationships, implement type checking utilities, or debug object structures. Essential for understanding how an object inherits methods and properties.
⚠️ Common Mistakes
Using __proto__ instead of Object.getPrototypeOf() - __proto__ is deprecated and should not be used in modern code.
Expecting getPrototypeOf to return the constructor - it returns the prototype object, not the constructor function.
Not handling null prototype - objects created with Object.create(null) return null, not an empty object.
✅ Best Practices
Use for instanceof-like checks that work across frames: Object.getPrototypeOf(obj) === Array.prototype
Create prototype chain inspection utilities for debugging: function getPrototypeChain(obj) { ... }
Prefer instanceof for type checking in most cases - it is more readable and handles the prototype chain automatically.
⚡ Performance Notes
Object.getPrototypeOf() is an O(1) operation that simply returns the internal prototype reference. It does not copy or traverse anything. Very fast and safe for use anywhere, including in performance-critical code.
🌍 Real World Example
Prototype Chain Inspector
Create a utility to inspect and visualize the complete prototype chain of any object for debugging and educational purposes.
function getPrototypeChain(obj) {
const chain = [];
let current = obj;
while (current !== null) {
const proto = Object.getPrototypeOf(current);
if (proto === null) {
chain.push({ type: 'null (end of chain)', proto: null });
} else {
const name = proto.constructor?.name || 'Anonymous';
chain.push({ type: name, proto });
}
current = proto;
}
return chain;
}
// Example 1: Array prototype chain
const arr = [1, 2, 3];
const arrayChain = getPrototypeChain(arr);
console.log('Array chain:');
arrayChain.forEach((item, i) => {
console.log(' ' + i + ': ' + item.type);
});
// Array chain:
// 0: Array
// 1: Object
// 2: null (end of chain)
// Example 2: Custom class inheritance
class Animal {
speak() { return 'sound'; }
}
class Dog extends Animal {
bark() { return 'woof'; }
}
const dog = new Dog();
const dogChain = getPrototypeChain(dog);
console.log('Dog chain:');
dogChain.forEach((item, i) => {
console.log(' ' + i + ': ' + item.type);
});
// Dog chain:
// 0: Dog
// 1: Animal
// 2: Object
// 3: null (end of chain)
// Verify prototype relationships
console.log(Object.getPrototypeOf(dog) === Dog.prototype); // true
console.log(Object.getPrototypeOf(Dog.prototype) === Animal.prototype); // true