Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Object.getPrototypeOf

Returns the prototype of an object.

Available since: ES5

Syntax

Object.getPrototypeOf(obj)

Parameters

ParameterTypeDescription
objobjectThe object whose prototype you need.

Return value

object | nullThe object's prototype, or null.

Examples

The prototype of an array and of a class instance:

class User {}
const anna = new User();
 
console.log(Object.getPrototypeOf([]) === Array.prototype);
console.log(Object.getPrototypeOf(anna) === User.prototype);

Output:

true
true

The prototype chain ends with null:

const obj = {};
 
console.log(Object.getPrototypeOf(Object.getPrototypeOf(obj)));

Output:

null

Notes

The old obj.__proto__ property does the same but is considered legacy. To check whether something is an instance, instanceof is more convenient.

See also

  • Object.create — Creates an object with the given prototype.
  • instanceof — Checks whether an object was created by a class (or a subclass).
  • class — Declares a class — a template for objects with shared methods.

In the tutorial

  • Prototypes — How inheritance works inside JavaScript: an object's prototype, the prototype chain, class methods in prototype and Object.create.

Thanks for reading!

Donate to the site — we work hard for you.