Javascript and automatic semicolon insertion

后端 未结 3 953
慢半拍i
慢半拍i 2021-01-27 22:54

test262 test suite has test containing source:

var x=0, y=0;
var z=
x
++
++
y

The annotation says:

Since LineTerminato

3条回答
  •  盖世英雄少女心
    2021-01-27 23:26

    This code will become:

    var z = x;
    ++ ++ y;
    

    The ++ ++ y is the root of the problem. Let’s look at why...

    ++ ++ y gets evaluated as ++(++y). The first step is to evaluate (++y). The ++ operator increments the value referenced by the variable it is next to, and returns the incremented value. The important part here is that it does not return a reference, just a value. So the second step would be ++(1), (or whatever ++y yielded), which is an error, since only references can be incremented.

提交回复
热议问题