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) {
};
6 个解决方案
#1
31
A function declaration does not need (and should not have) a semicolon following it:
函数声明不需要(也不应该有)分号后面的分号:
function test(o) {
}
However, if you write a function as a statement, 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) {
};
#2
6
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 error per-se. The browser treats it like a statement where absolutely nothing happened.
第二个分号不是错误本身。浏览器将其视为一个绝对没有发生任何事情的声明。
There are two things to keep in mind, here.
这里要记住两件事。
This applies ONLY to function-declarations and control-blocks (for/if/while/switch/etc).
这仅适用于函数声明和控制块(对于/ if / while / switch / etc)。
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.
因为浏览器会假设你的意思是函数test(){}(/ *返回值* /);这是一个错误。一个非常糟糕和令人讨厌的错误很容易被忽视。
But that's okay, because function-declarations can go under return statements and still work just fine.
但是没关系,因为函数声明可以在返回语句下进行,但仍然可以正常工作。
So even if you wanted to go:
所以,即使你想去:
function doStuff () {
return (function () { /*process stuff*/ test(); }());
function test () {}
}
That's going to work just peachy.
这只会起作用。
#3
3
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.
第一种方式定义了一个函数,但第二种方式是将函数赋给变量,因此是一个语句。大多数语句都以分号分隔。定义函数可以被认为是一个常见的反例,因为没有多少人使用它们。
#4
2
Semicolons and function declarations:
分号和函数声明:
function test(o) {
// body
} // semicolon no
var test = function (o) {
// body
}; // semicolon yes
See JSLint for formatting code questions.
请参阅JSLint以格式化代码问题。
#5
1
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
#6
0
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 need to add semicolons in between, example:
但有一个例外,如果你使用函数包装器并传递参数,你需要在它们之间添加分号,例如:
(function(v){alert(v)})('1');
(function(s){alert(s)})('0')
- or it will result in error: http://jsfiddle.net/twUD3/1/
- 或者它会导致错误:http://jsfiddle.net/twUD3/1/
... Otherwise forget about them ...
......别忘了他们......
#1
31
A function declaration does not need (and should not have) a semicolon following it:
函数声明不需要(也不应该有)分号后面的分号:
function test(o) {
}
However, if you write a function as a statement, 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) {
};
#2
6
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 error per-se. The browser treats it like a statement where absolutely nothing happened.
第二个分号不是错误本身。浏览器将其视为一个绝对没有发生任何事情的声明。
There are two things to keep in mind, here.
这里要记住两件事。
This applies ONLY to function-declarations and control-blocks (for/if/while/switch/etc).
这仅适用于函数声明和控制块(对于/ if / while / switch / etc)。
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.
因为浏览器会假设你的意思是函数test(){}(/ *返回值* /);这是一个错误。一个非常糟糕和令人讨厌的错误很容易被忽视。
But that's okay, because function-declarations can go under return statements and still work just fine.
但是没关系,因为函数声明可以在返回语句下进行,但仍然可以正常工作。
So even if you wanted to go:
所以,即使你想去:
function doStuff () {
return (function () { /*process stuff*/ test(); }());
function test () {}
}
That's going to work just peachy.
这只会起作用。
#3
3
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.
第一种方式定义了一个函数,但第二种方式是将函数赋给变量,因此是一个语句。大多数语句都以分号分隔。定义函数可以被认为是一个常见的反例,因为没有多少人使用它们。
#4
2
Semicolons and function declarations:
分号和函数声明:
function test(o) {
// body
} // semicolon no
var test = function (o) {
// body
}; // semicolon yes
See JSLint for formatting code questions.
请参阅JSLint以格式化代码问题。
#5
1
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
#6
0
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 need to add semicolons in between, example:
但有一个例外,如果你使用函数包装器并传递参数,你需要在它们之间添加分号,例如:
(function(v){alert(v)})('1');
(function(s){alert(s)})('0')
- or it will result in error: http://jsfiddle.net/twUD3/1/
- 或者它会导致错误:http://jsfiddle.net/twUD3/1/
... Otherwise forget about them ...
......别忘了他们......