Monday, May 20, 2024
128
rated 0 times [  135] [ 7]  / answers: 1 / hits: 21958  / 9 Years ago, wed, june 17, 2015, 12:00:00

Given this class; how would i iterate over the methods that it includes?



class Animal {
constructor(type){
this.animalType = type;
}
getAnimalType(){
console.log('this.animalType: ', this.animalType );
}
}

let cat = window.cat = new Animal('cat')


What I have tried is the following with no success:



for (var each in Object.getPrototypeOf(cat) ){
console.log(each);
}

More From » ecmascript-6

 Answers
10

You can use Object.getOwnPropertyNames on the prototype:



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

[#66181] Monday, June 15, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dequant

Total Points: 88
Total Questions: 99
Total Answers: 95

Location: Ukraine
Member since Sun, Dec 13, 2020
4 Years ago
dequant questions
;