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

1
  • 5
    it is important to note that arguments.callee is deprecated and unavailable in strict mode Commented Sep 13, 2012 at 18:57

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Managed to find on MDN a solution to calling a function within the function body here developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/…
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.