简体   繁体   English

为什么我应该在 javascript 中的每个函数后使用分号?

[英]Why should I use a semicolon after every function in javascript?

I've seen different developers include semicolons after functions in javascript and some haven't.我见过不同的开发人员在 javascript 中的函数后包含分号,有些则没有。 Which is best practice?哪个是最佳实践?

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

or或者

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

Semicolons after function declarations are not necessary .函数声明后的分号不是必需的

The grammar of a FunctionDeclaration is described in the specification as this: FunctionDeclaration的语法在规范中是这样描述

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不是statement

FunctionDeclarations are evaluated before the code enters into execution, hoisting is a common word used to explain this behaviour. FunctionDeclarations在代码进入执行之前被评估, 提升是用来解释这种行为的常用词

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:但是,在使用FunctionExpressions地方总是建议使用分号,例如:

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.第一个函数将立即执行,因为第二个函数周围的括号将被解释为函数调用的Arguments

Recommended lectures:推荐讲座:

I use them after function-as-variable declarations:我在函数作为变量声明之后使用它们:

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

but not after classical-style definitions:但不是在经典风格的定义之后:

function f() {
    ...
}

JS Lint is de-facto convention, and it says no semicolon after function body. JS Lint是事实上的约定,它表示函数体后没有分号。 See the "Semicolon" section.请参阅“分号”部分。

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 )。

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.我喜欢用分号结束代码行,因为我习惯了 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
     }
}

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.此外,当您压缩或缩小代码时,缺少分号可能会导致脚本的缩小版本无法执行您想要的操作,因为所有空白都消失了。

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.如果您将函数定义为 var,是的,您需要使用分号。

need semicolon需要分号

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

no need semicolon不需要分号

function x(){}

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.始终使用它们的优势之一是如果您想缩小您的 JavaScript。

As minifying the Javascript, helps to reduce the file size a bit.缩小 Javascript 有助于稍微减小文件大小。

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.如果您不使用分号,并且如果您想缩小(就像很多开发人员在他们的网站提供大量 JavaScript 时喜欢做的那样),您可能会收到各种错误/警告。

the semicolon after a function is not necessary using it or not, does not cause errors in your program.函数后的分号不需要使用或不使用,不会导致程序出错。 however, if you plan to minify your code, then using semicolons after functions is a good idea.但是,如果您打算缩小代码,那么在函数后使用分号是个好主意。 say for example you have code like the one below说例如你有像下面这样的代码

//file one
var one=1;
var two=2;
function tryOne(){}
function trytwo(){}

and

//file two
var one=1;
var two=2;
function tryOne(){};
function trytwo(){};

when you minify the both, you will get the following as output当您缩小两者时,您将获得以下输出

Note that comments are just for ilustration请注意,评论仅用于说明

//file one
var one=1;var two=2;function tryOne(){}
function trytwo(){}

and

//file two
var one=1;var two=2;function tryOne(){};function trytwo(){};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM