Only variables declared without var become global; this does not apply to functions.
You can, however, declare foo as follows:
foo = function() {}
and it must be global.
Lowering var usually not recommended for these reasons (from the top of the head):
- The variable resolution starts from the local one and goes to the search in the global namespace, which makes it slower. Much slower in some browsers.
- You typically have name conflicts, polluting the global namespace. One of the worst offenders will be, say,
for(i = 0; i < arr.length; i++) (note the lack of var )
You might want to declare functions with var because of a language function called hoisting
By the way, if you decide to declare functions with var , I recommend that you do this as follows:
var foo = function foo() {}
because it gives the function a "name" instead of processing an anonymous function, which will help with debugging. Most people do not, and declare using function , I suppose.
source share