当+ x ++工作正常时,为什么+++ x会给出错误消息?

Nar*_*ala 7 javascript

var x = null;
Run Code Online (Sandbox Code Playgroud)

+++x生成一个ReferenceError,但是当我使用postfix increment运算符执行相同操作时+x++,它的工作正常.

jAn*_*ndy 5

LeftHandSideExpression++经营者不得为数字.例如

1++;
Run Code Online (Sandbox Code Playgroud)

将失败并出现相同的错误(无效的增量操作数).您只能在变量/标识符/表达式上应用前后增量运算符.

由于+符号将null value数字转换为数字(0),因此结果相同.

例子:

var foo = null,
    bar = 5;

foo++;    // 0
0++;      // invalid increment operand
null++;   // invalid increment operand
(+bar)++  // invalid increment operand
foo++ +2; // 2
Run Code Online (Sandbox Code Playgroud)