Como obter o nome de uma função de chamada em JavaScript?
Considere o seguinte exemplo.
var obj = function(){};
function apply(target, obj) {
if (target && obj && typeof obj == "object") {
for (var prop in obj) {
target[prop] = obj[prop];
}
}
return target;
}
apply(obj.prototype, {
firstFunction: function (){
this.secondFunction();
},
secondFunction: function (){
// how do I know what function called me here?
console.log("Callee Name: '" + arguments.callee.name + "'");
console.log("Caller Name: '" + arguments.callee.caller.name + "'");
}
});
var instance = new obj();
instance.firstFunction();
ATUALIZAR
Ambas as respostas são realmente impressionantes. Obrigado. Então, examinei o problema de chamar uma função recursiva ou parent em um objeto e encontrei uma solução aqui. Isso permitiria que eu recuperasse o nome da função sem usar as propriedades arguments.callee / caller.
https://developer.mozilla.org/pt-BR/docs/JavaScript/Reference/Operators/function