【发布时间】:2021-12-28 21:37:44
【问题描述】:
鉴于这个类;我将如何迭代它包含的方法?
class Animal {
constructor(type){
this.animalType = type;
}
getAnimalType(){
console.log('this.animalType: ', this.animalType );
}
}
let cat = window.cat = new Animal('cat')
我尝试过以下但没有成功:
for (var each in Object.getPrototypeOf(cat) ){
console.log(each);
}
【问题讨论】:
-
不应该迭代
Animal.prototype工作吗?据我了解,底层对象架构仍然与“旧”方式相同。 -
Animal.prototype 公开了这些方法。我认为问题在于迭代它们的方式。 for.. in 似乎不起作用。