Object.getPrototypeOf
Returns the prototype of an object.
Available since: ES5
Syntax
Object.getPrototypeOf(obj)Parameters
| Parameter | Type | Description |
|---|---|---|
| obj | object | The 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
trueThe prototype chain ends with null:
const obj = {};
console.log(Object.getPrototypeOf(Object.getPrototypeOf(obj)));Output:
nullNotes
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.