Skip to content Skip to sidebar Skip to footer

How Could This Be True??? Obj2.__proto__.isprototypeof(obj2) //true

Consider this short code: let obj1 = { name: 'obj1', } const obj2 = Object.create(obj1); obj2.name = 'obj2' If you console.log(obj2), it will show this in Google Chrome (Versio

Solution 1:

When you do

let obj1 = {
  name: "obj1",
}

const obj2 = Object.create(obj1);

You're creating an obj2 with the following prototype chain:

Object.prototype -> obj1 -> obj2

(Both Object.protoype and obj1 are within obj2's internal prototype chain)

When you reference the __proto__ property on an object, this will point you to the internal prototype of the current object. So, for example, obj2.__proto__ is obj1.

(Although .__proto__ is deprecated, it's not inaccessible)

So

obj2.__proto__.isPrototypeOf(obj2) // true

is equivalent to

obj1.isPrototypeOf(obj2) // true

And obj1is indeed within obj2's internal prototype chain, so it evaluates to true.

Similarly, for

obj2.__proto__.__proto__.isPrototypeOf(obj1) // true

this is

obj2.__proto__.__proto__.isPrototypeOf(obj1) // true
          obj1.__proto__.isPrototypeOf(obj1) // true          Object.prototype.isPrototypeOf(obj1) // true

Which makes sense as well - Object.prototype is indeed within obj1's prototype chain.

It's better to use the non-deprecated version Object.getPrototypeOf instead of __proto__, they do the same thing:

let obj1 = {
  name: "obj1",
};
const obj2 = Object.create(obj1);

console.log(obj2.__proto__ === obj1);
console.log(Object.getPrototypeOf(obj2) === obj1);

Post a Comment for "How Could This Be True??? Obj2.__proto__.isprototypeof(obj2) //true"