Why should I use a semicolon after every function

2019-01-01 13:55发布

I've seen different developers include semicolons after functions in javascript and some haven't. Which is best practice?

function weLikeSemiColons(arg) {
   // bunch of code
};

or

function unnecessary(arg) {
  // bunch of code
}

8条回答
栀子花@的思念
2楼-- · 2019-01-01 14:47

It's actually more than an issue of convention or consistency.

I'm fairly certain that not placing semicolons after every statement slows down the internal parser because it has to figure out where the end of the statement is. I wish I had some handy numbers for you to positively confirm that, but maybe you can google it yourself. :)

Also, when you are compressing or minifying code, a lack of semi-colons can lead to a minified version of your script that doesn't do what you wanted because all the white space goes away.

查看更多
流年柔荑漫光年
3楼-- · 2019-01-01 14:50

Semicolons after function declarations are not necessary.

The grammar of a FunctionDeclaration is described in the specification as this:

function Identifier ( FormalParameterListopt ) { FunctionBody }

There's no semicolon grammatically required, but might wonder why?

Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement.

FunctionDeclarations are evaluated before the code enters into execution, hoisting is a common word used to explain this behaviour.

The terms "function declaration" and "function statement" are often wrongly used interchangeably, because there is no function statement described in the ECMAScript Specification, however there are some implementations that include a function statement in their grammar, -notably Mozilla- but again this is non-standard.

However semicolons are always recommended where you use FunctionExpressions, for example:

var myFn = function () {
  //...
};

(function () {
  //...
})();

If you omit the semicolon after the first function in the above example, you will get completely undesired results:

var myFn = function () {
  alert("Surprise!");
} // <-- No semicolon!

(function () {
  //...
})();

The first function will be executed immediately, because the parentheses surrounding the second one, will be interpreted as the Arguments of a function call.

Recommended lectures:

查看更多
低头抚发
4楼-- · 2019-01-01 14:50

Really just depends on your preference. I like to end lines of code with semi colons because I'm used to Java, C++, C#, etc, so I use the same standards for coding in javascript.

I don't typically end function declarations in semi colons though, but that is just my preference.

The browsers will run it either way, but maybe some day they'll come up with some stricter standards governing this.

Example of code I would write:

function handleClickEvent(e)
{
     // comment
     var something = true;  // line of code
     if (something)  // code block
     {
        doSomething();  // function call
     }
}
查看更多
琉璃瓶的回忆
5楼-- · 2019-01-01 14:51

Just stay consistent! They are not needed, but I personally use them because most minification techniques rely on the semi-colon (for instance, Packer).

查看更多
不再属于我。
6楼-- · 2019-01-01 14:51

SIMPLE:

It is a good practice to leave the semicolons ; after the end of function braces. They have been considered a best practice for years.

One advantage of always using them is if you want to minify your JavaScript.

As minifying the Javascript, helps to reduce the file size a bit.

But as for the best practise and answer above, not recommended to use it after a function tag.

If you DON'T use semicolons, and if you want to minify (like a lot of developers like to do if their site serves a lot of JavaScript) you could get all sorts of Errors/Warnings.

查看更多
忆尘夕之涩
7楼-- · 2019-01-01 14:53

When I minified my scripts I realized that I need to use semicolon for functions which starts with equals mark. if you define a function as var, yes you need to use semicolon.

need semicolon

var x = function(){};
var x = new function(){};
this.x = function(){};

no need semicolon

function x(){}
查看更多
登录 后发表回答