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

Asked

Viewed 118 times

1

For example:

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

executar(ola);

Thus, the execute function will execute (rsrs) the callback which, in this case, is the ola(). What I’d like to know is: how can I, within the function executar(), have access to the real name of callback (ola), since in this scope I only use the alias "callback" to reference any function passed as argument?

1 answer

4


To access the name: callback.name

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

executar(ola);
  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.