JavaScript(JS) object method - getPrototypeOf

https://‮figi.www‬tidea.com

The getPrototypeOf() method is a built-in method of the JavaScript Object constructor. It returns the prototype of a specified object.

Here's the syntax:

Object.getPrototypeOf(obj)

where obj is the object whose prototype we want to retrieve. This method takes only one argument, which is the object whose prototype we want to retrieve.

The prototype of an object is the object from which it inherits properties. In JavaScript, every object has a prototype, except for the root object (Object.prototype). The prototype is stored in the [[Prototype]] internal property of the object.

Here's an example that shows how to use the getPrototypeOf() method:

const person = {
  name: 'John',
  age: 30,
  greet() {
    console.log(`Hi, my name is ${this.name} and I'm ${this.age} years old`);
  }
};

const student = {
  major: 'Computer Science',
  year: 3
};

Object.setPrototypeOf(student, person);

console.log(Object.getPrototypeOf(student)); // {name: 'John', age: 30, greet: ƒ}

In this example, we have two objects person and student. We set the prototype of student to person using the Object.setPrototypeOf() method. Finally, we use the getPrototypeOf() method to retrieve the prototype of student.

Note that the getPrototypeOf() method only returns the prototype of an object, not the entire prototype chain. To retrieve the entire prototype chain, you can use the Object.getPrototypeOf() method in a loop.