【发布时间】:2015-07-27 16:51:53
【问题描述】:
我在 JavaScript 中有一个 for 循环,我已经通过 JSLint 运行了几次。过去我收到the unexpected++ error,我决定重构以使我的代码更具可读性。大约一个月后,JSLint 发布了更新,现在显示警告...
语句位置出现意外的表达式“i”。 for (i; i
//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
}
}
恢复到var i = 0 和i++ 不会改善警告,JSLint 只是停止处理。
【问题讨论】:
-
没有理由在 for 循环的第一部分中使用
i,如果您在其他地方进行声明和初始化,则将该部分留空并保留分号- -
直接在 Visual Studio 中删除导致警告并在 JSLint 中显示为
Unexpected 'for'.和Expected ';' and instead saw ')'.。我很确定 for 需要所有三个组件才能正常工作。 -
Visual Studio 在我个人看来是一个糟糕的 IDE。
for(;i<x;i++) statement;是一个完全有效的 for 循环,不管它告诉你什么。 -
如果你还是不信,看MDN docs for the
forloop -
您在哪里看到
var i=0; for (i; ...在 JSLint 中优于var i; for (i=0; ...?我不一定在 linked question 和 JSLint 说明中看到这一点。
标签: javascript jslint