¿Cómo obtener el nombre de una función de llamada en Javascript?
Considere el siguiente ejemplo.
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();
ACTUALIZAR
Ambas respuestas son realmente impresionantes. Gracias. Luego examiné el problema de llamar a una función recursiva o padre dentro de un objeto y encontré una solución aquí. Esto me permitiría recuperar el nombre de la función sin usar los argumentos.callee / caller properties.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/function