Course contentsShow
← JavaScript
Lesson 636 of 2,72315. ObjectsFree lesson

Object.getPrototypeOf()

Retrieve an object's prototype using Object.getPrototypeOf() to inspect the inheritance chain.

Object.getPrototypeOf()

You'll learn how to inspect an object's prototype to understand its inheritance chain.

What Is Object.getPrototypeOf()?

Every JavaScript object has an internal link to another object called its prototype. This prototype acts like a parent that the object can inherit properties and methods from. Object.getPrototypeOf() lets you retrieve that prototype object so you can examine what's behind the scenes.

Think of it like checking your family tree: you exist as an individual, but you also have a parent that gave you certain traits. Object.getPrototypeOf() reveals who that "parent" object is.

How It Works

The syntax is straightforward:

const proto = Object.getPrototypeOf(someObject);

For example, when you create an object literal, its prototype is Object.prototype:

const person = { name: "Alice" };
const proto = Object.getPrototypeOf(person);

console.log(proto === Object.prototype); // true

Arrays have Array.prototype as their parent:

const numbers = [1, 2, 3];
const arrayProto = Object.getPrototypeOf(numbers);

console.log(arrayProto === Array.prototype); // true

Why Use It?

You'd inspect prototypes to:

  • Understand where inherited methods come from
  • Debug unexpected behavior
  • Check what capabilities an object has through its prototype chain
  • Verify relationships between objects
const obj = {};
console.log(Object.getPrototypeOf(obj)); // { constructor: f, toString: f, ... }

Key Takeaway: Object.getPrototypeOf() reveals the prototype object that another object inherits from, helping you understand the inheritance chain and where properties and methods originate.