考虑到这一节课;我将如何迭代其中包含的方法?

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);
}

最佳答案

您可以在原型上使用Object.getOwnPropertyNames

Object.getOwnPropertyNames( Animal.prototype )
// [ 'constructor', 'getAnimalType' ]

关于javascript - ES6遍历类方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59151124/

10-12 06:57