何时使用分号

Dan*_*n K 2 javascript

特别是在JavaScript中,何时使用分号,何时使用分号.

这是一段代码示例;

function Drawable() {
   this.init = function(x, y) {
      this.x = x;
      this.y = y;
   }

   this.speed = 0;
   this.canvasWidth = 0;
   this.canvasHeight = 0;

   this.draw = function() {
   };
}
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我为什么

this.init = function(x,y) {}
Run Code Online (Sandbox Code Playgroud)

但是,并不以分号结尾

this.draw = function(){};
Run Code Online (Sandbox Code Playgroud)

在上面的代码片段中以分号结尾?

Ben*_*aum 8

这是自JavaScript运动自动分号插入以来的个人风格问题.:

当从左到右解析程序时,遇到任何语法产生不允许的令牌(称为违规令牌),然后在违规令牌之前自动插入分号,如果......违规令牌至少有一个LineTerminator与前一个标记分开.

第一个不以分号结尾,因为上面的代码不一致.

一致的方式是:

this.init = function(x, y) {
   this.x = x;
   this.y = y;
};
Run Code Online (Sandbox Code Playgroud)

是否应该使用它们的问题已经在SO中讨论过了.