我在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只是停止处理.
由于JSLint处于测试阶段,看起来后续问题不是[只是?].这是因为Crockford for
默认不再允许声明.看起来我需要留出一个周末阅读新的说明和来源.男人,圈子K正在发生奇怪的事情.
ES6最重要的新功能是正确的尾调用.这没有新的语法,所以JSLint没有看到它.但它使递归更具吸引力,这使得循环,尤其是循环,更不具吸引力.
然后在/*jslint */
指令部分的主表中:
描述:Tolerate
for
语句
选项:for
含义:true
如果for
允许该语句.
表格下面还有一些解释:
JSLint不建议使用该
for
语句.forEach
改为使用数组方法.该for
选项将禁止一些警告.for
JSLint接受的表单受到限制,不包括新的ES6表单.
因此,要在新的JSLint中创建此lint,至少需要此代码(使用for
指令集):
/*jslint white:true, for:true */
/*global scope, title */
function test()
{
"use strict";
var i;
for (i=0; i < scope.formData.tabs.length; i = 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)
请注意,我也仍然要移动i
的初始化,所以你可能仍然有一个问题值得报告.我也承认,在你联系的问题上,我和斯蒂芬在一起 ; 我不确定为什么i+= 1
会更好.但现在它看起来像一个艰难的要求.没有plusplus
选择.
另请注意,如果代码未包含在函数中(我包含在test
上面),您将获得Unexpected 'for' at top level.
,这是一个新错误.
归档时间: |
|
查看次数: |
4812 次 |
最近记录: |