It's considered "bad" because its behavior is inconsistent and sometimes doesn't do what most people would expect. Consider the following snippet:
if (true) {
function f() { return "t"; }
} else {
function f() { return "f"; }
}
What would calling f()
return? On IE and Firefox, it will return "t"
, but on Chrome and Safari, it will return "f"
. Why? A combination of hoisting and JavaScript's scoping rules. For information on how that works, see this question on JavaScript scoping and hoisting.
Effectively, this means that, on some browsers, the branch is completely ignored when it comes to function definitions using the function name() { ... }
form. If you really need to do something like this (and you probably don't, since it would be very poor style, anyway), use the name = function () { ... }
form instead, since that will not be hoisted, so the behavior is well-defined and will not be inconsistent across platforms.