++和 -
已知++(增量)和 - (减量)运算符通过鼓励过多的诡计来导致错误的代码.它们仅次于故障架构,可以实现病毒和其他安全威胁.有一个plusplus选项禁止使用这些运算符.
我知道像PHP这样的PHP结构$foo[$bar++]可能很容易导致一个错误,但是我无法找到一个更好的方法来控制循环而不是一个while( a < 10 ) do { /* foo */ a++; }或for (var i=0; i<10; i++) { /* foo */ }.
jslint是否突出显示它们是因为有些类似的语言缺少" ++"和" --"语法或者处理方式不同,还是有其他理由可以避免我可能会丢失的" ++"和" --"?
可能重复:
为什么要避免在JavaScript中增加("++")和减少(" - ")运算符?
jslint中的"意外的++"错误
jslint.com给了我错误:
Unexpected '++'.
Run Code Online (Sandbox Code Playgroud)
对于这一行:
for (i = 0; i < l; ++i) {
Run Code Online (Sandbox Code Playgroud)
我试过i++但没有去.
我在JavaScript中有一个for循环,我已经通过JSLint运行了几次.在我收到的过去the unexpected++ error,我决定重构以使我的代码更具可读性.大约一个月后,JSLint推出了更新,现在正在显示警告......
语句位置中出现意外表达式"i".for(i; i <scope.formData.tabs.length; i = i + 1){
//See JSLint.com for why I pulled out i initialization and i = i+1 instead of i++
//and http://stackoverflow.com/questions/3000276/the-unexpected-error-in-jslint
var i = 0;
for (i; i < scope.formData.tabs.length; i += 1) {
scope.formData.tabs[i].show = false; // hide all the other tabs
if (scope.formData.tabs[i].title === title) {
scope.formData.tabs[i].show = true; // show the new tab
}
}
Run Code Online (Sandbox Code Playgroud)
恢复var i = 0并且i++没有改进警告,JSLint只是停止处理.