我在答案中看到了
在 Javascript 中,为什么要写“var QueryStringToHash = function QueryStringToHash (query) { ... }”?
这正在做类似的事情
var foo = function foo(param) {
...
}
在那种特殊情况下,为什么要这样做而不是仅仅使用
function foo(param) {
...
}
? 这样做的好处或动机是什么?
我在答案中看到了
在 Javascript 中,为什么要写“var QueryStringToHash = function QueryStringToHash (query) { ... }”?
这正在做类似的事情
var foo = function foo(param) {
...
}
在那种特殊情况下,为什么要这样做而不是仅仅使用
function foo(param) {
...
}
? 这样做的好处或动机是什么?
In shortly, if you take the following code, the first example creates a function, named foo
, the second example creates an anonymous function and assign it to bar
variable. Besides style, the basic difference is that foo
can be called, in code, before it's definition (since it's the name of the function); otherwise, bar
is an undefined variable before it receives the assignment, thus cannot be used before.
var foo_result = foo(123); // ok
function foo(param) { /* ... */ }
var bar_result = bar(123); // error: undefined is not a function
var bar = function(param) { /* ... */ }
var bar_result = bar(123); // ok
I'd recommend you to read the suggestion of @Pekka.