JS: Object.prototype.__proto__ ❌
The Property __proto__
obj.__proto__
Return the parent of obj.
Object.prototype.__proto__ is both a getter and a setter property.
〔see Getter Setter Properties〕
🛑 WARNING:
Object.prototype.__proto__ is not standard part of JS.
It was invented by Firefox browser in 2000s, then supported by all browsers, and ECMAScript 2015 standardize the behavior, but not part of the JavaScript language.
Use of the property "__proto__" fails if the object does not have a parent, or if the key exist in parent chain.
Not supported in deno nor TypeScript
🟢 TIP: better is Reflect.getPrototypeOf and Reflect.setPrototypeOf .
// __proto__ is not supported in deno and other js engines console.log(([4, 6].__proto__) === undefined); // true // in deno
Set __proto__
obj.__proto__ = xParent
Set the parent of obj to xParent.
// set a obj's parent using __proto__ const aa = {}; const bb = {}; bb.__proto__ = aa; console.log((Reflect.getPrototypeOf(bb) === aa) === false); // true // in deno
Example of __proto__ Failure
// create two objects const aa = Object.create(null); const bb = Object.create(null); // set parent Reflect.setPrototypeOf(bb, aa); // verify the parent console.log(Reflect.getPrototypeOf(bb) === aa); // true // property __proto__ fails console.log((bb.__proto__ === aa) === false); // true // value is undefined console.log(bb.__proto__ === undefined); // true
What is the difference between getPrototypeOf vs .__proto__ Property
__proto__may fail when a object in its parent chain has a string key"__proto__".__proto__fails when object does not have a parent.__proto__is not supported in Deno, TypeScript.__proto__is not standard JavaScript, but is mentioned in appendix of JavaScript spec.__proto__works via inheritance over prototype object Object.prototype.__proto__- Reflect.getPrototypeOf and Reflect.setPrototypeOf always work, and are in JavaScript spec.