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

239
Views
Get method/function name in JavaScript class from method it self

I want to log an errors to know from which method/function of class i got error. To log this i want a generic code, by that i can know the method/function name from method it self. below is my code.

In below code, each catch block i want particular method name like SaveData and GetData.

I tried arguments.callee but it's not working in strict mode.

I also tried Object.values(this) but it gives me all methods array so i can't decide at what element my current method name exist.

I want any generic solution because i have hundreds of function in a class and i not want to write each function name in each method.

class MyClass {

    /**
    * Class default constructor
    */
    constructor() {
        this.file_path = path.join(__dirname, path.basename(__filename));
    }

    SaveData = async (req, res) => {
        try {
            //doing some operation to store data
        } catch (err) {
            //in catch block i want "SaveData"(method name) so i can pass as parameter in below generic method which i have created somewhere in common file.
            log.error(res, err, this.file_path);    
        }
    }

    GetData = async (req,res) => {
        try {
            //doing some operation to get records from db
        } catch (err) {
            //in catch block i want "GetData"(method name) so i can pass as parameter in below generic method which i have created somewhere in common file.
            log.error(res, err, this.file_path);      
        }
    }
}

module.exports = new MyClass();
about 4 years ago · Juan Pablo Isaza
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!