The Javascript getPrototypeOf() is an inbuilt function in JavaScript that is used to check the prototype of an object that the user has created. Most of the time, It is used to check whether two objects have the same prototype or not. The prototype here means the internal definition of the object that is defined by the user in the JavaScript code.
Syntax:
Object.getPrototypeOf(created_object);
Parameters: It accepts a parameter “created object” which is the object of the entity whose prototype going to be found out.
Return Value: It returns the internal prototype of the object passed in the method.
JavaScript examples to show the working of the getPrototypeOf() function:
Example 1: This example shows the basic use of the getPrototypeOf() function in javascript.
javascript
// Creating a simple function function myfun() { } // creating a new object let obj = new myfun(); // getting the prototype console.log(Object.getPrototypeOf(obj)); |
Output:
[object Object]
Example 2: Another application of the getPrototypeOf() function is to check whether two objects have the same prototype or not.
javascript
// Creating a simple function let first_var = function myFun() { }; // Creating a object let second_var = Object.create(first_var); // Getting the output console.log(Object.getPrototypeOf (second_var === first_var)); |
Output:
false
Supported Browser :
- Chrome
- Edge
- Firefox
- Opera
- Safari