【问题标题】:Accessing the `__proto__` property isn't getting what I would expect it to get Javascript访问 `__proto__` 属性并没有得到我期望的 Javascript
【发布时间】:2020-09-25 16:44:56
【问题描述】:

所以,我试图理解这段代码:

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__)
*/

所以,我不明白的是,我希望 test.__proto__ 等于:

breathing: "yes"
__proto__: Object

因为test对象的__proto__键等于那个,如“Mark:1”日志所示,对吧? Object.getProtoTypeOf(test) 记录相同。我错过了什么?

【问题讨论】:

  • test 不是Animal,那么为什么它的__proto__ 应该是Animal.prototype

标签: javascript prototype


【解决方案1】:

让我们看看下一行。

Object.getPrototypeOf(Object.getPrototypeOf(oCat))

Object.getPrototypeOf(oCat)new Mamal()

所以我们可以说Object.getPrototypeOf(Object.getPrototypeOf(oCat)) 等于Object.getPrototypeOf(new Mamal())

Object.getPrototypeOf(new Mamal())Mamal,因为new Mamal() 返回一个原型为Mamal 的新对象。

这就是为什么在调用console.log("Mark:1", test) 时会看到Mamal

【讨论】:

  • 哎呀!我实际上省略了一些代码!是的,我原以为原型会按照您刚才所说的方式进行布局,但我的日志显示并非如此。后来,我有一些代码将Animal() 对象添加到原型链中。我只是忘记了对象可变性因素,它记录了 oCat 后来的内容,而不是记录的那一刻,这让我感到困惑,但我现在完全明白了,谢谢你的回答!
猜你喜欢
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 2014-11-01
  • 1970-01-01
  • 2015-12-01
相关资源
最近更新 更多