Reflect.getPrototypeOf(target) returns the prototype of an object: the object that it inherits properties and methods from. For an array this is Array.prototype, and for an object made with a class it is that class's prototype.
If the object has no prototype, you get null. By calling the method again on the result, you can walk up the whole prototype chain until you reach null.
Remember
Reflect.getPrototypeOf(target)
returns the prototype object, or null
Reflect.getPrototypeOf([]) → Array.prototype
Reflect.getPrototypeOf({}) → Object.prototype
The end of every chain → null
target must be an object, or TypeError
Syntax
Pass an object. Compare the result with the prototype you expect.
JavaScript
const list = [1, 2, 3];
const proto = Reflect.getPrototypeOf(list);
console.log(proto === Array.prototype); // true
console.log(Reflect.getPrototypeOf(proto) === Object.prototype); // true
// ECMAScript 2015 (ES6)
Reflect.getPrototypeOf(target)
// target : an object
// returns: the prototype object, or null
// throws : TypeError if target is not an object
Parameters
Parameter
Description
target
Required. The object whose prototype you want. If it is not an object, a TypeError is thrown.
Return Value
Return Value
Description
Object
The prototype of target.
null
target has no prototype, for example an object made with Object.create(null), or the end of a prototype chain.
TypeError (thrown)
target is not an object.
JavaScript Examples
Example 1 — Find the Prototype of an Object
Check the prototype of an array, a plain object, a function and an object that has no prototype.
Array: true
Object: true
Function: true
No prototype: null
How It Works
Built-in types. Each kind of value has a matching prototype: arrays use Array.prototype, plain objects use Object.prototype, and functions use Function.prototype.
Compare with ===. The prototype is an object, so you compare it by identity with the one you expect.
No prototype at all.Object.create(null) makes an object without a prototype, so you get null.
Example 2 — Prototypes of Classes
Create a small class family and see how the prototypes are connected.
JavaScript
class Animal {}
class Dog extends Animal {}
const rex = new Dog();
console.log("Instance: " + (Reflect.getPrototypeOf(rex) === Dog.prototype));
console.log("Dog.prototype: " + (Reflect.getPrototypeOf(Dog.prototype) === Animal.prototype));
console.log("Class Dog: " + (Reflect.getPrototypeOf(Dog) === Animal));
console.log("Class Animal: " + (Reflect.getPrototypeOf(Animal) === Function.prototype));
Instance: true
Dog.prototype: true
Class Dog: true
Class Animal: true
How It Works
Instance to class. The prototype of rex is Dog.prototype, the object that holds the methods of Dog.
Up to the parent. The prototype of Dog.prototype is Animal.prototype, because Dog extends Animal.
Classes are objects too. The prototype of the class Dog itself is Animal, which is how static methods are inherited. The class Animal has Function.prototype.
Example 3 — Walk Up the Prototype Chain
Repeat getPrototypeOf() in a loop until null. Then see that primitives are not accepted. The catch block prints the error type only.
JavaScript
const names = [];
let current = Reflect.getPrototypeOf([1, 2, 3]);
while (current !== null) {
names.push(current.constructor.name);
current = Reflect.getPrototypeOf(current);
}
console.log("Chain: " + names.join(" -> "));
try {
Reflect.getPrototypeOf(5);
} catch (err) {
console.log("Reflect with a number: " + err.name);
}
console.log("Object with a number: " + (Object.getPrototypeOf(5) === Number.prototype));
Chain: Array -> Object
Reflect with a number: TypeError
Object with a number: true
How It Works
Loop to the end. The code reads the prototype again and again until it gets null, and saves the name of each constructor on the way.
The chain of an array. An array inherits from Array.prototype, which inherits from Object.prototype, which has no prototype.
Primitives.Reflect.getPrototypeOf(5) throws a TypeError, while Object.getPrototypeOf(5) quietly wraps the number and returns Number.prototype.
Compatibility
Browser & Runtime Support
Reflect.getPrototypeOf() is part of ES2015 (ES6), together with the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
Reflect.getPrototypeOf()
Supported in Chrome 49+, Edge 12+, Firefox 42+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox42+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
Reflect.getPrototypeOf()Excellent
Bottom line: Safe to use in every current browser and in Node.js. The only thing to plan for is Internet Explorer, which has no Reflect at all.
Frequently Asked Questions
It returns the prototype of an object, the object that it inherits from. If the object has no prototype, it returns null.
For objects they give the same result. Object.getPrototypeOf() accepts primitives and converts them to objects first. Reflect.getPrototypeOf() throws a TypeError when the target is not an object.
It is the list of objects that JavaScript searches when you read a property. If the object does not have it, JavaScript looks at its prototype, then at the prototype of that, and so on until null. Reflect.getPrototypeOf() lets you walk this list.
No. Every object has a prototype that you read with getPrototypeOf(). Only functions and classes have a prototype property, which is the object their instances will inherit from.
Because some objects have no prototype. Object.prototype is at the end of the normal chain and has none, and Object.create(null) makes a new object without one.