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

251
Views
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 answers
Answer question

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 Report
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!