为什么在javascript中的每个函数后面都要使用分号?

时间:2022-09-10 23:09:56

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

我见过不同的开发人员在javascript中包含分号后的函数,有些则没有。最佳实践是什么?

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

or

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

8 个解决方案

#1


382  

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.

分号用于将语句彼此分开,而FunctionDeclaration不是语句。

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.

术语“函数声明”和“函数语句”经常被错误地互换使用,因为ECMAScript规范中没有描述函数语句,但是有些实现在语法中包含函数语句,尤其是Mozilla,但这又是不标准的。

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:

推荐讲座:

#2


33  

I use them after function-as-variable declarations:

我在函数声明后使用它们:

var f = function() { ... };

but not after classical-style definitions:

但不是在古典风格定义之后:

function f() {
    ...
}

#3


20  

JS Lint is de-facto convention, and it says no semicolon after function body. See the "Semicolon" section.

JS Lint是事实上的约定,它说函数体之后没有分号。请参见“分号”部分。

#4


7  

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

只是保持一致!它们是不需要的,但我个人使用它们,因为大多数缩小技术依赖于分号(例如,Packer)。

#5


4  

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.

这完全取决于你的喜好。我喜欢用semi冒号结束代码行,因为我习惯了Java、c++、c#等等,所以我在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
     }
}

#6


3  

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.

此外,当您在压缩或缩小代码时,缺少半冒号会导致脚本的缩小版本无法完成所需的工作,因为所有的空白都消失了。

#7


1  

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.

当我缩小我的脚本时,我意识到我需要对函数使用分号,它以= mark开头。如果您定义一个函数为var,那么您需要使用分号。

need semicolon

需要分号

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

no need semicolon

不需要分号

function x(){}

#8


0  

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 major advantage of always using them is if you want to minify your JavaScript.

总是使用它们的一个主要优点是如果您想要缩小JavaScript。

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

作为对Javascript的简化,有助于减少文件的大小。

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.

如果你不使用分号,如果你想缩小(就像很多开发人员喜欢做的那样,如果他们的站点提供了大量的JavaScript),你会得到各种各样的错误/警告。

#1


382  

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.

分号用于将语句彼此分开,而FunctionDeclaration不是语句。

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.

术语“函数声明”和“函数语句”经常被错误地互换使用,因为ECMAScript规范中没有描述函数语句,但是有些实现在语法中包含函数语句,尤其是Mozilla,但这又是不标准的。

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:

推荐讲座:

#2


33  

I use them after function-as-variable declarations:

我在函数声明后使用它们:

var f = function() { ... };

but not after classical-style definitions:

但不是在古典风格定义之后:

function f() {
    ...
}

#3


20  

JS Lint is de-facto convention, and it says no semicolon after function body. See the "Semicolon" section.

JS Lint是事实上的约定,它说函数体之后没有分号。请参见“分号”部分。

#4


7  

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

只是保持一致!它们是不需要的,但我个人使用它们,因为大多数缩小技术依赖于分号(例如,Packer)。

#5


4  

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.

这完全取决于你的喜好。我喜欢用semi冒号结束代码行,因为我习惯了Java、c++、c#等等,所以我在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
     }
}

#6


3  

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.

此外,当您在压缩或缩小代码时,缺少半冒号会导致脚本的缩小版本无法完成所需的工作,因为所有的空白都消失了。

#7


1  

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.

当我缩小我的脚本时,我意识到我需要对函数使用分号,它以= mark开头。如果您定义一个函数为var,那么您需要使用分号。

need semicolon

需要分号

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

no need semicolon

不需要分号

function x(){}

#8


0  

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 major advantage of always using them is if you want to minify your JavaScript.

总是使用它们的一个主要优点是如果您想要缩小JavaScript。

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

作为对Javascript的简化,有助于减少文件的大小。

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.

如果你不使用分号,如果你想缩小(就像很多开发人员喜欢做的那样,如果他们的站点提供了大量的JavaScript),你会得到各种各样的错误/警告。