The Object.getPrototypeOf function in JavaScript retrieves the prototype of a given object, which corresponds to the value of its internal [[Prototype]] property.
Syntax:
Example
Object.getPrototypeOf(obj)
Parameter
obj : This refers to an object for which the prototype is intended to be returned.
Return value:
This function provides the prototype associated with the specified object. In cases where there are no inherited properties, this function will yield a null value.
Browser Support:
| Chrome | 5 |
|---|---|
Edge |
Yes |
| Firefox | 3.5 |
| Opera | 12.1 |
Example 1
Example
let animal = {
eats: true
};
// create a new object with animal as a prototype
let rabbit = Object.create(animal);
alert(Object.getPrototypeOf(rabbit) === animal); // get the prototype of rabbit
Object.setPrototypeOf(rabbit, {}); // change the prototype of rabbit to {}
Output:
Example 2
Example
const prototype1 = {};
const object1 = Object.create(prototype1);
const prototype2 = {};
const object2 = Object.create(prototype2);
console.log(Object.getPrototypeOf(object1) === prototype1);
console.log(Object.getPrototypeOf(object1) === prototype2);
Output:
Output
true
false
Example 3
Example
const prototype1 = {};
const object1 = Object.create(prototype1);
const prototype2 = {};
const object2 = Object.create(prototype2);
console.log(Object.getPrototypeOf(object1) === prototype1);
console.log(Object.getPrototypeOf(object2) === prototype2);
Output:
Output
true
true