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

427
Views
how to Pass a JavaScript function name as parameter and replace function name like parameter

Its just an experiment

I can pass function name ass parameter but it's called the same name function.

function refreshContactList(id) {
    console.log(`Hello World ${id}`);
}

function addContact(id, refreshCallback) {
    refreshCallback(id);
}

addContact(1, refreshContactList);

like that.


but I want to change the function name, so its changes the name of the function like that ( image )

function addContact(id, refreshCallback) {
    
    function refreshCallback(id) {
        console.log(`Hello World ${id}`);
    }
    
}

addContact(1, refreshContactList);

is there any way to achieve that?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Is Nested Functions you want?

function addContact(id) {
    function anotherThing() {
        console.log(`Hello World ${id}`);
    }
    
    return { anotherThing };

}

addContact(1).anotherThing();
about 4 years ago · Juan Pablo Isaza Report

0

In your picture, you pass not name of function but the reference to this function. If you really want to call the top-level function by the given name (that is, by the string with the name of this function), you can use a cross-platform method that works equally well in node.js, browsers (traditionally, except for Internet Explorer) and other platforms: globalThis:

function a() {
  console.log('a')
}

function b() {
  console.log('b')
}

function callByName(name) {
  globalThis[name]
}

callByName(a)
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!