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

146
Views
List all class methods in Javascript

The best way to explain my problem is through the below code:

class SomeClass {
  SomeMethod() {
    console.log("SomeMethod was called");
  }
}

var someInstance = new SomeClass();

// The below expression expected to return True but is returns False
console.log(
  someInstance.hasOwnProperty("SomeMethod")
);

// The below expression is expected to include SomeMethod in the return list, but it doesn't
console.log(
  Object.keys(someInstance)
);

// The below expression is expected to include SomeMethod in the return list, but it doesn't
console.log(
  Object.getOwnPropertyNames(someInstance)
);

// The below expression works fine which confrms that the function exists.
someInstance.SomeMethod();

CodeSandbox link

I would expect Object.keys() or Object.getOwnPropertyNames() to return all properties including methods defined in the class definition of the class from which this object was constructed but for some reason neither does.

If neither of these functions works in such a scenario then how can I list all methods of a class instance?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You Can create custom function to acheive this

class SomeClass {
  SomeMethod() {
    console.log("SomeMethod was called");
  }
}

var someInstance = new SomeClass();
function getMethods(obj) {
  return Object.getOwnPropertyNames(Object.getPrototypeOf(obj))
    .filter(m => 'function' === typeof obj[m])
}
console.log(getMethods(someInstance))

about 4 years ago · Juan Pablo Isaza 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!