我不明白为什么在使用 JavaScript 文件运行 JSLint 时会收到错误消息。
我收到消息 var os_map = {};第 28 行第 36 个字符的问题:如果我运行 this code,请使用数组文字符号 [].
在 JSLint . JSLint 的选项如下。
/*jslint onevar: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */
声明对象(即 {}
)应该没问题,但 JSLint 建议使用空数组(即 []
)
:我找到了答案。我错了。 var os_map = {}
没有任何问题。该代码显示在错误消息中,因为我没有使用 "require strict";
。我得到错误消息错误。感谢您回答我的问题。
最佳答案
违规行:
var os_autoload_inputs = new Array('searchInput', 'searchInput2',
'powerSearchText', 'searchText');
JSLint 做 not expect要查看 new Array
构造函数,您应该改用 []:
var os_autoload_inputs = ['searchInput', 'searchInput2',
'powerSearchText', 'searchText'];
为什么? :
1,Crockford 不喜欢new
。
2、Array
对象可以被覆盖:
Array = {};
new Array(); // TypeError: Array is not a constructor
3、用法不一致,例如:
var a = new Array(5); // empty 5 elements array
var b = [5]; // 1 element array containing the 5 number on index 0
另见:
关于javascript - "Use the array literal notation []"对于 var os_map = {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1936047/