should a multiline var f = function() { /* Code */ } get a semicolon

ε祈祈猫儿з 提交于 2020-01-15 07:42:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!