3

I've read the numerous posts here on stack overflow about how to use

arguments.callee.caller or arguments.callee.caller.toString()

If you do a simple search, you can find a bunch of posts about this.

So, i decided to try this myself! However, I'd like to find the name of the function inside of a javascript object, but it seems to be returning the code inside of the function, instead of the actual function name.

Here is the jsFiddle: http://jsfiddle.net/4Cp5Z/18/.

To summarize, I make a new app, then call app.testFunction. testFunction() calls receiver(), and I'm expecting var name to be equal to testFunction

app = function() {

    this.testFunction = function() {

        this.receiver();

    };

    this.receiver = function() {

        var name = this.receiver.caller.toString();
        // this.receiver.caller.name outputs nothing...

    };
};
Kara
  • 6,115
  • 16
  • 50
  • 57
Jeff
  • 1,800
  • 8
  • 30
  • 54
  • 2
    What you're doing looks like a pretty serious "code smell". What are you *really* trying to achieve? – Pointy Dec 28 '12 at 23:29
  • I'm really just trying to learn how to get the name of the of the function (or named function expression) that called my current function. I'm doing this for logging/ error handling purposes. – Jeff Dec 28 '12 at 23:50
  • In that case [you should check this out](http://www.eriwen.com/javascript/js-stack-trace/). – Pointy Dec 28 '12 at 23:55

1 Answers1

3

None of the functions in your example has a name. They're all anonymous. The variables and/or properties you're assigning them to have names, but the functions do not. For a function to have a true name, there's a name after the function keyword and the opening parenthesis:

// Declaration
function nameIsHere() {
}

// Expression -- but beware of these ("named function expressions"), see below
var f = function nameIsHere() {
};

(Note that IE8 and earlier create two separate functions if you use a named function expression.)

arguments.callee gives you a reference to the function object from within a call to that function, on engines that support it, and if you're not using strict mode. Beware that it carries a significant performance hit (an order of magnitude on some engines), and it is not allowed in strict mode.

arguments.callee.caller gives you a reference to the function that called the current function (if you're not in strict mode). It doesn't give you its name, but instead an actual reference.

Function#toString has no behavior specified in the specification, but on all major desktop browsers, it gives you some representation of the source code of the function. (This is not necessarily true of mobile and niche browsers.) But again, note that this is non-standard, and varies from browser to browser (for instance, some return the source code fairly verbatim, including comments; others return a decompiled representation of their compiled function).

There's a non-standard Function#name property supported in many, but not all, engines, which gives you the true name of the function (if it has one). But again, it has never been standardized, not even in ES5, and your functions don't have names.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I read this answer, and this was all very helpful. --However, in the case of `var f = function() { //code here }`, is there any way to get the 'expression name' (in this case f) from the function that f called? – Jeff Dec 28 '12 at 23:48
  • 1
    @1__ no, there's no special relationship between a function instantiated like that and some random variable that's assigned a reference to it. – Pointy Dec 29 '12 at 00:19
  • 1
    @1__: As Pointy said, no, there's no in-language way to do that. The development tools in some browsers (recent versions of Chrome and Firebug) are smart enough to figure it out for showing you in call stacks and breakpoint lists, but that's a feature of the tools, not something you can access from code -- not least because what if you have `foo(obj.bar = delta = function() { ... });`? What's the "name" of that, `obj.bar`? `delta`? The name of the `foo` argument we're passing it in as? :-) – T.J. Crowder Dec 29 '12 at 07:54