简体   繁体   English

访问 `__proto__` 属性没有得到我期望它获得 Javascript

[英]Accessing the `__proto__` property isn't getting what I would expect it to get Javascript

So, there's this code that I'm trying to make sense of:所以,我试图理解这段代码:

function Mammal() {
  this.isMammal = 'yes';
}

function MammalSpecies(sMammalSpecies) {
  this.species = sMammalSpecies;
}

MammalSpecies.prototype = new Mammal();
MammalSpecies.prototype.constructor = MammalSpecies;

var oCat = new MammalSpecies('Felis');
console.log(oCat.isMammal); // 'yes'

function Animal() {
  this.breathing = 'yes';
}

var test = Object.getPrototypeOf(Object.getPrototypeOf(oCat))
console.log("Mark:1", test) /* output
{constructor: ƒ}
constructor: ƒ Mammal()
__proto__: Animal
breathing: "yes"
__proto__: Object*/
console.log("Mark:1.3", test.__proto__) /* output
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
*/
console.log("Mark:2", Object.getPrototypeOf(test)) /* output
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
(same as test.__proto__)
*/

 function Mammal() { this.isMammal = 'yes'; } function MammalSpecies(sMammalSpecies) { this.species = sMammalSpecies; } MammalSpecies.prototype = new Mammal(); MammalSpecies.prototype.constructor = MammalSpecies; var oCat = new MammalSpecies('Felis'); console.log(oCat.isMammal); // 'yes' function Animal() { this.breathing = 'yes'; } var test = Object.getPrototypeOf(Object.getPrototypeOf(oCat)) console.log("Mark:1", test) /* output {constructor: ƒ} constructor: ƒ Mammal() __proto__: Animal breathing: "yes" __proto__: Object*/ console.log("Mark:1.3", test.__proto__) /* output {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …} constructor: ƒ Object() hasOwnProperty: ƒ hasOwnProperty() isPrototypeOf: ƒ isPrototypeOf() propertyIsEnumerable: ƒ propertyIsEnumerable() toLocaleString: ƒ toLocaleString() toString: ƒ toString() valueOf: ƒ valueOf() __defineGetter__: ƒ __defineGetter__() __defineSetter__: ƒ __defineSetter__() __lookupGetter__: ƒ __lookupGetter__() __lookupSetter__: ƒ __lookupSetter__() get __proto__: ƒ __proto__() set __proto__: ƒ __proto__() */ console.log("Mark:2", Object.getPrototypeOf(test)) /* output {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …} constructor: ƒ Object() hasOwnProperty: ƒ hasOwnProperty() isPrototypeOf: ƒ isPrototypeOf() propertyIsEnumerable: ƒ propertyIsEnumerable() toLocaleString: ƒ toLocaleString() toString: ƒ toString() valueOf: ƒ valueOf() __defineGetter__: ƒ __defineGetter__() __defineSetter__: ƒ __defineSetter__() __lookupGetter__: ƒ __lookupGetter__() __lookupSetter__: ƒ __lookupSetter__() get __proto__: ƒ __proto__() set __proto__: ƒ __proto__() (same as test.__proto__) */

So, what I don't understand, is, I would expect test.__proto__ to be equal to:所以,我不明白的是,我希望test.__proto__等于:

breathing: "yes"
__proto__: Object

Because the __proto__ key of the test object is equal to that, as shown in the "Mark:1" log, right?因为test对象的__proto__键等于那个,如“Mark:1”日志所示,对吗? Object.getProtoTypeOf(test) logs the same. Object.getProtoTypeOf(test)记录相同。 What am I missing?我错过了什么?

Let's take a look into the next line.让我们看看下一行。

Object.getPrototypeOf(Object.getPrototypeOf(oCat))

Object.getPrototypeOf(oCat) is new Mamal() . Object.getPrototypeOf(oCat)new Mamal()

So we can say that Object.getPrototypeOf(Object.getPrototypeOf(oCat)) is equal to Object.getPrototypeOf(new Mamal()) .所以我们可以说Object.getPrototypeOf(Object.getPrototypeOf(oCat))等于Object.getPrototypeOf(new Mamal())

And Object.getPrototypeOf(new Mamal()) is Mamal because new Mamal() return a new object with prototype Mamal . Object.getPrototypeOf(new Mamal())Mamal因为new Mamal()返回一个带有原型Mamal的新对象。

So this is why you see Mamal when calls for console.log("Mark:1", test) .所以这就是为什么你在调用console.log("Mark:1", test)时会看到Mamal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM