为什么在新行没有为方括号自动插入分号(ASI)?

时间:2018-09-09 09:10:09

标签: javascript ecmascript-6

我读了一篇文章说,以下5例没有ASI。前4种情况很容易理解,但我没有5种情况。为什么第5种情况有效?

   //1. treated as plus operation
   a = b
   +a

   //2. treated as minus operation
   a = b
   -a

   //3. treated as division operation
   a = b
   /something/.test(a)

   //4. treated as function invocation
   a = b
   (function () {})()

   //5. treated as property access
   a = b
   [1, 2, 3].forEach() 

另一个例子是函数表达式

a = function() {

}
[1,2,3].forEach(function(item) {

});

//It will be interpreted as following,  but why is this valid at all?

a = function() {
}[1,2,3].forEach(function(item) {

});

为什么对函数express完全有效?为什么只为方括号添加ASI?

PS,在问问题之前,我确实读过What are the rules for JavaScript's automatic semicolon insertion (ASI)?。但是我看不到有关方括号的问题,尤其是对于函数表达式而言。

0 个答案:

没有答案