Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda