How do I get the name of a callback function in javascript?

1

For example:

function ola() {
    console.log("Olá =)");
}
function executar(callback) {
    // quero descobrir o nome deste callback que é passado para cá
   callback();
}

executar(ola);

In this way, the execute function will execute (rsrs) the callback, which in this case is the ola() function. What I would like to know is: how can I, within the executar() function, have access to the actual callback (hello) name, since in that scope I just use the alias "callback" to reference any function passed as argument?

    
asked by anonymous 06.09.2017 / 08:23

1 answer

4

To access the name: callback.name

function ola() {
    console.log("Olá =)");
}
function executar(callback) {
   console.log(callback.name);
   callback();
}

executar(ola);
    
06.09.2017 / 10:19