最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do get the name of a calling function in Javascript? - Stack Overflow

programmeradmin1浏览0评论

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.

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/en-US/docs/JavaScript/Reference/Operators/function

Share Improve this question edited Sep 14, 2012 at 4:51 Matt asked Sep 13, 2012 at 18:48 MattMatt 6,42210 gold badges57 silver badges85 bronze badges 1
  • 5 it is important to note that arguments.callee is deprecated and unavailable in strict mode – Elias Van Ootegem Commented Sep 13, 2012 at 18:57
Add a ment  | 

2 Answers 2

Reset to default 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.

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

发布评论

评论列表(0)

  1. 暂无评论