Object.getPrototypeOf(obj) returns the prototype of obj — the object it inherits properties and methods from. If there is no prototype, the result is null.
Call it again on the result to walk up the prototype chain. This is different from a constructor’s .prototype property, which is what instances inherit from.
Syntax
Call Object.getPrototypeOf() like this. Start with this line.
JavaScript
Object.getPrototypeOf(obj)
Call
What it does
Object.getPrototypeOf(obj)
Returns the prototype of obj, or null.
Object.getPrototypeOf(Object.getPrototypeOf(obj))
Moves one step further up the prototype chain.
Built into JavaScript. Call on Object — no import required.
Library declarations
JavaScript
Object.getPrototypeOf(obj)
JavaScript
getPrototypeOf(o: any): object | null;
Parameters
Parameter
Description
obj
Required. The value whose prototype you want. Primitives are boxed (for example a number becomes a Number object).
Return Value
Return Value
Description
object | null
The prototype object, or null when the object has no prototype (for example Object.prototype or Object.create(null)).
JavaScript Examples
Example 1 — Basic Use
Check the prototypes of a plain object and an array.
JavaScript
var person = { name: "Ada" };
var list = [1, 2, 3];
console.log(Object.getPrototypeOf(person) === Object.prototype);
console.log(Object.getPrototypeOf(list) === Array.prototype);
console.log(Object.getPrototypeOf(Object.prototype));
Create a bare objectObject.create(null) makes an object with no prototype link.
Own data still works You can set and read own properties like color as usual.
No inherited methods There is no toString from Object.prototype, so typeof dict.toString is "undefined".
Example 3 — Walk the Chain
Follow prototypes upward until you reach null.
JavaScript
function Animal() {}
function Dog() {}
Dog.prototype = Object.create(Animal.prototype);
var rex = new Dog();
console.log(Object.getPrototypeOf(rex) === Dog.prototype);
console.log(Object.getPrototypeOf(Dog.prototype) === Animal.prototype);
console.log(Object.getPrototypeOf(Animal.prototype) === Object.prototype);