javascript 函数声明后是否需要分号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11978698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:57:18  来源:igfitidea点击:

Do we need a semicolon after function declaration?

javascript

提问by Adam Lee

Possible Duplicate:
JavaScript: When should I use a semicolon after curly braces?

可能的重复:
JavaScript:我什么时候应该在花括号后使用分号?

Someone added semicolon after function declaration, but someone not. Is this a good practice to add semicolon after function declaration?

有人在函数声明后添加了分号,但有人没有。这是在函数声明后添加分号的好习惯吗?

function test(o) {
}

function test(o) {
};

回答by Lee

A function declarationdoes not need (and should not have) a semicolon following it:

一个函数声明不需要(也不应该)跟在它后面的分号:

function test(o) {
}

However, if you write a function as an expression, like the variable initializer below, then the statement should be terminated with a semicolon, just like any other statement would be:

但是,如果您将函数编写为表达式,就像下面的变量初始值设定项一样,那么该语句应该以分号终止,就像任何其他语句一样:

var a = function test(o) {
};

See more about constructor vs declaration(statement) vs expression.

查看更多关于构造函数、声明(语句)和表达式的信息

回答by Norguard

What's actually happening there is you're adding an empty statement after the function.

实际发生的情况是您在函数后添加了一个空语句。

function test (o) { return o; };

could be seen as being similar to:

可以被视为类似于:

var test = 0;;

That second semicolon isn't an errorper-se. The browser treats it like a statement where absolutely nothing happened.

第二个分号本身并不是错误。浏览器将其视为绝对没有发生任何事情的声明。

There are two things to keep in mind, here.

这里有两件事要记住。

This applies ONLYto function-declarations and control-blocks (for/if/while/switch/etc).

这适用ONLY到函数声明和控制块(/ IF /同时/开关/等)。

Function-declarations should be defined at the bottom of your scope, so you don't run into problems like this:

函数声明应该在你的范围的底部定义,这样你就不会遇到这样的问题:

function test () {}
(function (window, document, undefined) { /* do stuff */ }(window, document));

Because the browser will assume that you mean function test() {}(/*return value of closure*/);Which is an error. A very bad and nasty error which is very easy to overlook.

因为浏览器会假设你的意思是function test() {}(/*return value of closure*/);哪个是错误的。一个非常糟糕和令人讨厌的错误,很容易被忽视。

But that's okay, because function-declarations can go under return statements and still work just fine.

但这没关系,因为函数声明可以在 return 语句下进行并且仍然可以正常工作。

So even if you wanted to go:

所以即使你想去:

function doStuff () {
    return (function () { /*process stuff*/ test(); }());
    function test () {}
}

That's going to work just peachy.

这将奏效。

回答by Kendall Frey

No.

不。

You don't need semicolons when defining a function like that.

定义这样的函数时不需要分号。

However, if you define a function like this:

但是,如果您定义这样的函数:

var test = function (o) {
}

It's not strictly necessary, but you may want to use them, especially if you put the function on one line.

这不是绝对必要的,但您可能想要使用它们,尤其是当您将该函数放在一行时。

The first way defines a function, but the second way assigns a function to a variable, and thus is a statement. Most statements are semicolon delimited. Defining functions could be considered a common counterexample, as not many people do use them.

第一种方式定义了一个函数,而第二种方式将一个函数赋值给一个变量,因此是一个语句。大多数语句都以分号分隔。定义函数可以被认为是一个常见的反例,因为没有多少人使用它们。

回答by artlung

Semicolons and function declarations:

分号和函数声明:

function test(o) {
    // body
} // semicolon no

var test = function (o) {
    // body
}; // semicolon yes

See JSLintfor formatting code questions.

有关格式化代码问题,请参阅JSLint

回答by Polyov

To the browser, it doesn't matter. For matter of semantics, it only matters if you're prototyping a function or using the function statement.

对浏览器来说,没关系。对于语义问题,仅当您正在对函数进行原型设计或使用函数语句时才重要。

function stuff(stuff) {
     alert(stuff);
} //don't need a semicolon

Object.prototype.stuff = function(stuff) {
    alert(stuff);
}; //need a semicolon
var stuff = function(stuff) {
    alert(stuff);
}; //need a semicolon

回答by Anonymous

Semicolon is not required while defining a function, but putting it on is not a mistake either.

定义函数时不需要分号,但加分号也不是错误。

One exception though, if you use function wrappers and pass the parameters, you needto add semicolons in between, example:

但是有一个例外,如果您使用函数包装器并传递参数,则需要在它们之间添加分号,例如:

(function(v){alert(v)})('1');
(function(s){alert(s)})('0')

... Otherwise forget about them ...

... Otherwise forget about them ...