问题
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 (Version 79.0.3945.88 (Official Build) (64-bit)):
{name: "obj2"}
name: "obj2"
__proto__:
name: "obj1"
__proto__:
constructor: ƒ Object()
Or, you better check this console screenshot image:
From what Google Chrome presents, it is obvious that first proto of obj2 is obj1. It is logical too. How come then, that this is true:
obj2.__proto__.isPrototypeOf(obj2) // true
Also, how this is true:
obj2.__proto__.__proto__.isPrototypeOf(obj1) // true
And another thing. If ordinary object in JS, does not have prototype property (but internal prototype slot which is inaccessible), why .isPrototypeOf(obj2) is not undefined?? Because if you do obj2.prototype that's what you'll get.
I googled and googled this but to no avail.
回答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 obj1
is 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);
来源:https://stackoverflow.com/questions/59491618/how-could-this-be-true-obj2-proto-isprototypeofobj2-true