Object.getPrototypeOf()
Introduction
Object.getPrototypeOf() is a built-in JavaScript method that allows you to retrieve the prototype of an object. The prototype is an object that is used as a template for creating new objects using the same constructor function. Understanding how prototypes work is essential for creating efficient and maintainable code in JavaScript.
How it works ?
The Object.getPrototypeOf()
method returns the prototype of an object. It takes one parameter, which is the object whose prototype you want to retrieve. If the object has a prototype, the method returns it. If the object does not have a prototype
, the method returns null
. Here's an example:
function Person(name) {
this.name = name;
}
const john = new Person("John");
console.log(Object.getPrototypeOf(john)); // Output: Person {}
Examples
Here are some examples to illustrate how to use Object.getPrototypeOf()
:
function Vehicle(make, model) {
this.make = make;
this.model = model;
}
const car = new Vehicle("Toyota", "Camry");
Object.setPrototypeOf(car, Vehicle.prototype);
console.log(Object.getPrototypeOf(car) === Vehicle.prototype); // Output: true
const animal = {};
Object.setPrototypeOf(animal, Object.prototype);
console.log(Object.getPrototypeOf(animal) === Object.prototype); // Output: true
Detailed explanation
Syntax
Object.getPrototypeOf(obj);
Parameter
The Object.getPrototypeOf()
method takes only one parameter, "obj
", which is required. The "obj
" parameter is the object whose prototype you want to retrieve.
Return Value
The Object.getPrototypeOf()
method returns the prototype of the specified object. If the object has a prototype, the method returns it. If the object does not have a prototype, the method returns null
.
Limitations
The Object.getPrototypeOf()
method can only retrieve the prototype of an object. It cannot be used to modify the prototype of an object. If you want to modify the prototype of an object, you need to use the Object.setPrototypeOf() method. Additionally, This method only works with objects. It cannot be used with primitive values like strings, numbers, or booleans.
Common use cases:
This method is commonly used to inspect and modify the prototype of an object. It is also useful for checking whether an object inherits from a specific prototype.
Tips and tricks:
The Object.getPrototypeOf()
method is part of the prototype chain in JavaScript. It is used to access and manipulate the prototype object of an object. This method is similar to the proto
property, which is deprecated in favor of Object.getPrototypeOf()
.
When using this method, it is important to remember that modifying the prototype object of an object can have unintended consequences. It is best to avoid modifying the prototype object of built-in objects like Array, String, and Number, as this can cause compatibility issues with other libraries and frameworks.
When checking if an object inherits from a specific prototype, you can use the instanceof
operator instead of the Object.getPrototypeOf()
method. For example:
function Person(name) {
this.name = name;
}
const john = new Person("John");
console.log(john instanceof Person); // Output: true
Ecmascript specification
The Object.getPrototypeOf() method was introduced in ECMAScript 5.1 and is part of the core JavaScript language. It is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge.
This page was updated on -
Found an error or have feedback on our docs?
Create an issue on GitHub and let us know! Your input helps improve our documentation for everyone in the community.
Report error, send feedback on Github