花括号后我应该何时使用分号?

Rob*_*ons 60 javascript syntax

很多次我看到在函数声明之后使用分号,或者在模块模式脚本的匿名"返回"函数之后使用分号.花括号后用分号是否合适?

cle*_*tus 96

您在声明后使用分号.这是一个声明:

var foo = function() {
  alert("bar");
};
Run Code Online (Sandbox Code Playgroud)

因为它是一个变量赋值(即创建一个匿名函数并为其赋值).

想到的不是语句的两件事是函数声明:

function foo() {
  alert("bar");
}
Run Code Online (Sandbox Code Playgroud)

和块:

{
  alert("foo");
}
Run Code Online (Sandbox Code Playgroud)

注:同一个块构建无分号也适用于for,dowhile循环.

  • @Robusto这是一个变量赋值. (4认同)
  • 我知道@SLaks,它需要使用分号,因为这是变量赋值(这是我的观点)。 (2认同)
  • 这也适用于“if”语句。 (2认同)

ale*_*lex 23

当您打算缩小代码时,这也很重要.

所以我个人}在ASI插入一个之后添加一个.

在JavaScript中写了一篇关于ASI的帖子.


Web*_*ner 17

不要使用分号:

......如果只是你的日常功能声明:

function foo() {

} // No semicolon
Run Code Online (Sandbox Code Playgroud)


使用分号:

......如果是作业:

var foo = function() {

}; // Semicolon
Run Code Online (Sandbox Code Playgroud)


...或自我调用功能:

(function () {

})(); // Semicolon
Run Code Online (Sandbox Code Playgroud)


SLa*_*aks 10

你永远不需要; 你总是可以(除了之前elsewhile).

说明:

不幸的是,Javascript分号是可选的.
因此,您永远不需要添加分号.

用分号终止每个语句是非常好的做法.
唯一以a }结尾的语句是以对象文字(例如JSON)或函数表达式结尾.

因此,最佳做法是在以下两个括号之后添加分号(仅限):

var myFunc = function() { };
var myobject = { };
Run Code Online (Sandbox Code Playgroud)


小智 5

如果我们有一个自调用函数,我们需要在它之前加一个分号,否则它将成为前一个赋值语句的一部分.考虑以下:

testClass = function(name) {
  document.write ("Instantiating testClass<br />");
  this.name = name;
}

testClass.prototype.report = function() {
  document.write ("I'm " + this.name + "<br />");
  return 1;
}

testClass.prototype.testMethod = function(param) {
  document.write ("Running testMethod with parameter value " + param + "<br />");
  return 2;
} // notice that there is no semicolon here

(function() {
  document.write ("Running self-invoking function<br />");
  return 3;
}());

if (typeof(testClass.prototype.testMethod) !== "function") {
  document.write ("testMethod type: " + typeof(testClass.prototype.testMethod));
  document.write (", value: " + testClass.prototype.testMethod + "<br />");
}
var testOb = new testClass("Bill");
testOb.report();
testOb.testMethod(4);
Run Code Online (Sandbox Code Playgroud)


这将产生以下输出:

"运行自调用函数
运行带参数值为3的
testMethod testMethod类型:数字,值:2
实例化testClass
我是比尔"

...加上浏览器报告的JavaScript错误:testOb.testMethod is not a function

这肯定不是我们想要的.testMethod在我们甚至实例化类之前,为什么要立即运行?当我们想把它称为成员方法时,为什么它不再存在?

发生的事情是testMethod分配的不是我们的函数定义,而是函数定义的返回值.并且函数定义本身是匿名运行的.这是如何:

  1. testClass构造和部件的方法report被成功地定义/分配.
  2. 由于在定义之后缺少分号testMethod,所以()周围的下面的自调用函数变成了一个调用操作符,这导致我们认为我们的定义 testMethod成为一个立即调用的匿名函数,并返回值以下匿名函数成为其参数列表.这解释了打印输出的顺序 - 我们的自调用函数首先运行,因为它被评估为参数.
  3. 由于我们的预期函数定义返回2,因此分配给它的是2 testMethod,而不是函数定义.我们印刷的类型和价值证实了这一点testMethod.
  4. 现在testClass已成功实例化testOb,其report方法按预期工作,证明类定义完整无缺.
  5. 当我们尝试调用时testMethod,解释器告诉我们它不是函数 - 这是正确的,因为它是一个值为2的数字.

如果我们在定义之后放了一个分号testMethod,它会将它的赋值与调用自调用函数分开,我们将得到我们期望的结果:

"运行自调用函数
实例化testClass
我是Bill
运行testMethod参数值为4"



或者我们甚至可以直接将它放在匿名函数之前:

;(function() {...
Run Code Online (Sandbox Code Playgroud)

但我建议,由于问题是由于赋值语句末尾缺少分号,我们应该习惯在以这种方式定义函数后总是加分号.即上面的所有函数在结束括号后应该有一个分号,因为它们都是匿名函数的赋值.