views:

56

answers:

2

Is it possible to somehow set a name for anonymous functions?

There is no need to add function names to the namespace for anonymous functions but I would like to avoid seeing a large amount of (?) in my javascript debugger so I can keep the call stack trace informative.

Also can I safely pass normal declared functions as arguments instead of anonymous functions or will I walk into some strange errors. It seems to work.

$("object").bind("click", function() { alert("x"); });

$("object").bind("click", function debuggingName() { alert("x"); });

[Edit]

I meant something along the likes of

$("object").bind("click", function() { Function.Name = "debuggingName"; alert("x"); });
A: 

An anonymous function is a function without a name, it is executed from where it is defined. Alternatively, you can define the debugging function before using it.

function debuggingName() { 
    alert("x"); 
}

$("object").bind("click", debuggingName);
Q_the_dreadlocked_ninja
+1  A: 

Your second example is using a named function expression, which works fine in most browsers but has some problems in IE that you should be aware of before using it. I recommend reading kangax's excellent article on this subject.

Tim Down
Thank you, I was worried that passing a named function expression may cause errors.
Raynos
Oh that's a lovely article. I didn't notice there was actaully a difference between named function expressions and function declaration.
Raynos
Yes, I was just writing a comment pointing that out :)
Tim Down
Although actaully I believe my second example above is a function statement. Unles you count assigning the function declaration to arguments[1] as a named function expression. And I was starting to get a feeling that I understood javascript.
Raynos
Your second example is definitely a named function expression. It looks identical to a function declaration except for the context: a function declaration isn't valid where an expression is expected (such as when passing a function parameter, as your example does). A function statement is another thing entirely: that's a Mozilla extension to ECMAScript to prevent a function declaration within, say, an `if` block from being a syntax error. kangax's article covers that too: http://kangax.github.com/nfe/#function-statements
Tim Down
Oh bah I read the article wrong. Yes its a function expression. Out of curiosity does the phrase "named function expression" apply to such statements as var name = function thing() {} ? Or generally when are function expressions considered "named"
Raynos
Yes, the right hand side of `var name = function thing() {}` is a named function expression. `var name = function() {}` is just a function expression. Distinguishing between named function expressions, function statements and function declarations is all based on context. If it's a place where the interpreter expects an expression (e.g. in a variable declaration) then it'll be a named function expression. If it's in global code or within a function (but not inside a block in either case), it's a function declaration. If it's within a block (e.g. an `if` block), it's a function statement.
Tim Down