Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
How to enumerate es6 class methods

How can I enumerate methods of an ES6 class? similar to Object.keys

Here's an example:

class Callbacks {
  method1() {
  }

  method2() {
  }
}

const callbacks = new Callbacks();

callbacks.enumerateMethods(function(method) {
  // method1, method2 etc.
});
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Object.keys() iterates only enumerable properties of the object. And ES6 methods are not. You could use something like getOwnPropertyNames(). Also methods are defined on prototype of your object so you'd need Object.getPrototypeOf() to get them. Working example:

for (let name of Object.getOwnPropertyNames(Object.getPrototypeOf(callbacks))) {
    let method = callbacks[name];
    // Supposedly you'd like to skip constructor
    if (!(method instanceof Function) || method === Callbacks) continue;
    console.log(method, name);
}

Please notice that if you use Symbols as method keys you'd need to use getOwnPropertySymbols() to iterate over them.

over 4 years ago · Santiago Trujillo Report

0

There is no iterator / getter method like Object.keys in ES6 (yet?). you can, however, use for-of to iterate over the keys:

function getKeys(someObject) {
    return (for (key of Object.keys(someObject)) [key, someObject[key]]);
}

for (let [key, value] of getKeys(someObject)) {
    // use key / value here
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!