ghasemkiani avatar

JavaScript Basics: Object.getPrototypeOf and Object.setPrototypeOf

ghasemkiani

Published: 29 Jan 2018 › Updated: 29 Jan 2018JavaScript Basics: Object.getPrototypeOf and Object.setPrototypeOf

JavaScript Basics: Object.getPrototypeOf and Object.setPrototypeOf

These functions, as is evident from their names, enable you to get and set the prototype of an object. In light of the availability of these functions, the use of the __proto__ property is generally discouraged.

Here is an example:

    let objA = {};
    let objB = {};
    Object.setPrototypeOf(objA, objB);
    Object.getPrototypeOf(objA) === objB; // true

The prototype of an object can be null. So you can assign null as the prototype of an object.

It should be noted that many JavaScript engines perform optimizations based on the value of [[prototype]] internal property. Changing the prototype with Object.setPrototypeOf will have adverse effects on those optimizations even after the prototype has been set. Therefore, in cases where performance is very important, the use of Object.create is more efficient than changing the prototype of an object with Object.setPrototypeOf.


Related Posts

Leave JavaScript Basics: Object.getPrototypeOf and Object.setPrototypeOf to:

Written by

Read more #javascript posts


Best Posts From ghasemkiani

We have not curated any of ghasemkiani's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From ghasemkiani