问题
When setting a variable to reference a function in a multi-line declaration, should it be followed by a semicolon. Take the folling for example:
function sayHi(name) {
(window.console && console.log || alert)('Hello ' + name);
}
var sayBye = function(name) {
(window.console && console.log || alert)('Cya ' + name);
};
Should there be a semicolon after the second declaration as it fits with a var keyword, on the other hand, function definitions don't usually have a semicolon at the end.
回答1:
The first function is a function declaration, not a statement.
It does not need a semicolon.
The second function is a regular statement that happens to involve a function expression.
Like all other statements, it should end with an optional semicolon.
回答2:
It does not need a semicolon thanks to JavaScript parser magic, but Douglas Crockford would have a hernia if you didn't use one. In other words, it's considered good coding style to use a semicolon after assigning an anonymous function to a variable.
来源:https://stackoverflow.com/questions/4515084/should-a-multiline-var-f-function-code-get-a-semicolon