cis*_*iso 3 javascript loops jslint pre-increment post-increment
array.prototype.reduce函数位于:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
它有以下循环:
for (index = 0; length > index; ++index) {
if (this.hasOwnProperty(index)) {
if (isValueSet) {
value = callback(value, this[index], index, this);
} else {
value = this[index];
isValueSet = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不认为索引在这里是先增加还是后增加,因为它是在每次循环迭代之后完成的,但是想要确定.
可以将其更改为index + = 1,以便传递jslint吗?请不要讨论jslint警告的优点.
这种变化会有什么不同吗?
p.s*_*w.g 10
唯一的区别是i++,++i和,i += 1是从表达式返回的值.考虑以下:
// Case 1:
var i = 0, r = i++;
console.log(i, r); // 1, 0
// Case 2:
var i = 0, r = ++i;
console.log(i, r); // 1, 1
// Case 3:
var i = 0, r = (i += 1);
console.log(i, r); // 1, 1
Run Code Online (Sandbox Code Playgroud)
在这些情况下,i增量后保持不变,但r不同,i += 1只是稍微冗长的形式++i.
在你的代码中,你根本没有使用返回值,所以不,没有区别.就个人而言,我更喜欢使用,i++除非有特殊需要使用其他形式之一.