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

257
Vistas
In JavaScript, can you reference a method/function name within that method/function?

I am building an automation framework in JavaScript for WebdriverIO.

The "out-of-the-box" error messages aren't too helpful and I would like to add the class name and method name as part of a prefix to the error message which is thrown when the method/function fails.

I have managed to call the class name with ClassName.name

However, I have not found a solution to reference the method name without using some hardcoded value.

Below is a summary of the changes I would like to make.

Before:

setElementValue(element, value){
    try{
        this.waitForElementAndScroll(element);
        $(element).setValue(value);
    }
    catch(error){
        throw `${BasePage.name}.setElementValue: ${error.message}`;
    }
}

After:

setElementValue(element, value){
    try{
        this.waitForElementAndScroll(element);
        $(element).setValue(value);
    }
    catch(error){
        throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
    }
}

Where ${setElementValue.name} is the method of referencing the name of the method/function.

Many thanks in advance!

I have tried the following with no success:

method.name //ReferenceError: method is not defined

(method).name //ReferenceError: method is not defined

Function.name //Returns "Function"

Function.value //Returns "undefined"

constructor.name //Returns "Object"

setElementValue.name //ReferenceError: setElementValue is not defined

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I found this (very obvious) solution to work for me:

this.setElementValue.name

where the thrown error message is:

throw `${BasePage.name}.${this.setElementValue.name}: ${error.message}`;

which outputs the following error log when the method fails:

`BasePage.setElementValue: element ("#submit-button") still not displayed after 500ms`
about 4 years ago · Juan Pablo Isaza Denunciar
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