在循环中它是无害的,但在赋值语句中它可能导致意想不到的结果:
var x = 5;
var y = x++; // y is now 5 and x is 6
var z = ++x; // z is now 7 and x is 7
变量和运算符之间的空格也会导致意外结果:
a = b = c = 1; a ++ ; b -- ; c; console.log('a:', a, 'b:', b, 'c:', c)
在闭包中,意外的结果也可能是一个问题:
var foobar = function(i){var count = count || i; return function(){return count++;}}
baz = foobar(1);
baz(); //1
baz(); //2
var alphabeta = function(i){var count = count || i; return function(){return ++count;}}
omega = alphabeta(1);
omega(); //2
omega(); //3
它会在换行符后触发自动分号插入:
var foo = 1, bar = 2, baz = 3, alpha = 4, beta = 5, delta = alpha
++beta; //delta is 4, alpha is 4, beta is 6
前增量/后增量混淆会产生极难诊断的逐一错误。幸运的是,它们也完全没有必要。有更好的方法将 1 添加到变量中。
参考