Javascript 放;在函数定义的末尾

Javascript 放;在函数定义的末尾,javascript,jslint,Javascript,Jslint,为什么认为最好的做法是提出建议;在函数定义的末尾 e、 g 优于: var tony = function () { console.log("hello there"); } 您发布了一个函数表达式。与块不同,语句以分号结尾,因此您应该在那里插入分号,而不是依赖ASI隐式地为您执行 根据下面的代码,不添加分号可能会导致意外行为。TL;DR:如果没有分号,函数表达式可以根据后面的代码转换为立即调用的函数表达式 自动插入分号是一种痛苦。你不应该依赖它: var tony = functi

为什么认为最好的做法是提出建议;在函数定义的末尾

e、 g

优于:

var tony = function () {
   console.log("hello there");
}
您发布了一个函数表达式。与块不同,语句以分号结尾,因此您应该在那里插入分号,而不是依赖ASI隐式地为您执行


根据下面的代码,不添加分号可能会导致意外行为。

TL;DR:如果没有分号,函数表达式可以根据后面的代码转换为立即调用的函数表达式


自动插入分号是一种痛苦。你不应该依赖它:

var tony = function () {
   console.log("hello there"); // Hint: this doesn't get executed;
};
(function() {
  /* do nothing */
}());
与:

var tony = function () {
   console.log("hello there"); // Hint: this gets executed
}
(function() {
  /* do nothing */
}());

在第二个(坏)示例中,分号不会被插入,因为它后面的代码可能有意义。因此,您期望分配给tony的匿名函数会立即被调用,并使用一些其他内容作为参数,
tony
被分配给您期望的
tony
的返回值,这实际上不是您想要的。

两者似乎是相同的。。第一个说你的陈述已经结束。。第二个不确定,但JavaScript会自动添加分号。所以它是一个“防御性分号”。对我来说,这是一种迫使编辑器重新调整函数代码的方法
var tony = function () {
   console.log("hello there"); // Hint: this gets executed
}
(function() {
  /* do nothing */
}());