4

Consider the following example.

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();

UPDATE

Both answers are really awesome. Thank you. I then looked into the problem of calling a recursive, or parent function within an object and found a solution here. This would allow me to retrieve the function name without using the arguments.callee/caller properties.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/function

Matt
  • 6,264
  • 10
  • 54
  • 82

2 Answers2

4

Give name to the functions

like:

 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   firstFunction(){
        this.secondFunction();
    },
    secondFunction: function    secondFunction(){
        // 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();

take a look on this question

Community
  • 1
  • 1
Anoop
  • 23,044
  • 10
  • 62
  • 76
4

A function's name is an immutable property of that function, set in the initial function expression.

var notTheName = function thisIsTheName() { ... }

someObj.stillNotTheName = function stillTheName() { ... }

If your function expression does not have a name, there is (unsurprisingly) no way to identify it by name. Assigning a function to a variable does not give it a name; if that were the case, you could not determine the name of an expression assigned to multiple variables.

You should set firstFunction's name property by expressing it as

firstFunction: function firstFunction(){
    this.secondFunction();
}

Also, arguments.callee is deprecated. See Why was the arguments.callee.caller property deprecated in JavaScript? for a very good explanation of the history of arguments.callee.

Community
  • 1
  • 1
apsillers
  • 112,806
  • 17
  • 235
  • 239
  • My next question would be, if arguments.callee is deprecated then what is a good solution/work-around? I read the history in the link you provided, however I didn't find an acceptable solution. – Matt Sep 14 '12 at 04:00
  • Managed to find on MDN a solution to calling a function within the function body here https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/function – Matt Sep 14 '12 at 04:47