Sub topic 4
Reading and Changing Prototypes: getPrototypeOf, setPrototypeOf, and __proto__
This chapter assumes you know how the prototype chain resolves a property lookup and how Object.create sets a prototype when an object is created. Here you will learn how to read an existing object's [[Prototype]] and how to change it, along with the older __proto__ syntax you will still see in real code.
Reading a Prototype
Object.getPrototypeOf(obj) returns the object that obj links to, or null at the end of the chain. You can call it on any object, including objects that do not inherit from Object.prototype.
const base = { greet() { return 'hi'; } };
const obj = Object.create(base);
console.log(Object.getPrototypeOf(obj) === base); // true
console.log(Object.getPrototypeOf(base) === Object.prototype); // true
console.log(Object.getPrototypeOf(Object.prototype)); // nullChanging a Prototype
Object.setPrototypeOf(obj, proto) replaces the link on an object that already exists. proto must be an object or null. The function returns obj.
const base = { greet() { return 'hi'; } };
const other = { greet() { return 'hello'; } };
const obj = Object.create(base);
Object.setPrototypeOf(obj, other);
console.log(obj.greet()); // "hello"
const a = {};
const b = Object.create(a);
try {
Object.setPrototypeOf(a, b); // would make a cycle: a -> b -> a
} catch (err) {
console.log(err instanceof TypeError); // true
}JavaScript rejects any change that would create a cycle in the chain. It also throws a TypeError if you try to change the prototype of a non-extensible object, such as one passed through Object.freeze or Object.preventExtensions.
The Legacy `__proto__` Accessor
__proto__ is a getter/setter defined on Object.prototype. Reading it calls getPrototypeOf, and assigning to it calls setPrototypeOf. It works only because the object inherits it, so it fails on objects that do not inherit from Object.prototype:
const base = { greet() { return 'hi'; } };
const normal = {};
normal.__proto__ = base;
console.log(normal.greet()); // "hi"
const bare = Object.create(null);
bare.__proto__ = base; // no accessor here: creates an own property
console.log(Object.getPrototypeOf(bare)); // null
console.log(Object.keys(bare)); // ['__proto__']The __proto__ key inside an object literal is a separate feature. { __proto__: base } sets the prototype at creation time. It is standardized, fast, and does not depend on the accessor.
const base = { greet() { return 'hi'; } };
const viaLiteral = { __proto__: base, name: 'lit' };
console.log(Object.getPrototypeOf(viaLiteral) === base); // trueWhich to Use
| Tool | Purpose | Recommendation |
|---|---|---|
| `Object.getPrototypeOf` | Read the link | Use it for all reads |
| `Object.setPrototypeOf` | Change the link | Use only when you have no alternative |
| `obj.__proto__` (accessor) | Read or change the link | Avoid: it is legacy and fails on null-prototype objects |
| `{ __proto__: p }` (literal) | Set the link at creation | Fine to use |
Reflect.getPrototypeOf and Reflect.setPrototypeOf do the same jobs. The difference is that Reflect.setPrototypeOf returns false on failure instead of throwing.
Exercise
Given const d = Object.create(null); d.__proto__ = Array.prototype;, what does Array.isArray(d) return, what does Object.getPrototypeOf(d) return, and why?
Show answer
Array.isArray(d) returns false and Object.getPrototypeOf(d) returns null. Because d has no prototype, it never inherits the __proto__ accessor, so the assignment just creates an own property named "_proto_". (Array.isArray would return false even if the prototype had changed, because d is not a real array.) To actually set the link you would use Object.setPrototypeOf(d, Array.prototype).
Exercise
What does Object.setPrototypeOf(Object.freeze({}), {}) do?
Show answer
It throws a TypeError. A frozen object is non-extensible, so its prototype cannot be changed to a different object.