When should I use a semicolon after curly braces?

2019-01-04 23:56发布

Many times I've seen a semicolon used after a function declaration, or after the anonymous "return" function of a Module Pattern script. When is it appropriate to use a semicolon after curly braces?

8条回答
看我几分像从前
2楼-- · 2019-01-05 00:32

You also should use a semicolon after a curly bracket after returning a function within a function in Javascript.

function watchOut(problem) {
  return function(number, location) { 
    alert("Be careful! There are " + problem +
          " today!\n" +

          number + " have been spotted at the " + location + "!"
    );
  };
}
查看更多
我只想做你的唯一
3楼-- · 2019-01-05 00:33

Don't use a semicolon:

...if it's just your every-day function declaration:

function foo() {

} // No semicolon


Use a semicolon:

...if it's an assignment:

var foo = function() {

}; // Semicolon


...or a self invoking function:

(function () {

})(); // Semicolon
查看更多
登录 后发表回答