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
.