Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在IIFE前加分号以及是否使用分号 #1

Open
Jiavan opened this issue Mar 20, 2017 · 0 comments
Open

在IIFE前加分号以及是否使用分号 #1

Jiavan opened this issue Mar 20, 2017 · 0 comments

Comments

@Jiavan
Copy link
Collaborator

Jiavan commented Mar 20, 2017

一般会看到在这样的模块下会在之前加上一个分号或者加号

;(function() {
  // code
});

之所以这样是因为在代码块压缩的时候不加分号可能会出现问题,比如:

// dev
var a = 0
(function() {})();

// uglify
var a=0 (function(){})(); // error

所以在IIFE前加上一个分号作为语句结束的分隔符,这是一个好的习惯,在bootstrap的一些插件中也使用+在IIFE之前。

由于js有ASI(自动分号插入),在语句后是否应该加分号也是一个比较开放的话题,但是最好有一个统一约定。更多讨论查看https://www.zhihu.com/question/20298345

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant