While debugging I found that this kind of functions:
var f = function() {};
Appear on the stack trace of firebug or webkits dev console as anonymous, and rightfully so.
Also I’ve seen people defining these as:
var someName = function otherName(){};
Which are quite weird. Note that here you cant call otherName() from anywhere but the body of otherName itself. From everywhere else you have to use someName().
My questions are:
-
Is there any problem in naming a function different from the var where it’s stored?
-
Does
var a = function a(){}makes any difference besides just showing the name in the stack trace ? -
Any other tip/suggestion on this topic 🙂
There’s no problem with assigning a function named f to a variable named a.
A nice reference on functions is https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope. Of particular interest is the section entitled “Function constructor vs. function declaration vs. function expression” which has a detailed discussion the on distinction between the function name and the variable the function is assigned to. (You may have seen this already.)
My guess is the reason that the debugger prints something like
is that the function’s name appears when the function value itself is serialized. The debugger is giving you all the information it has.