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

253
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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