Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda