"对var os_map = {}使用数组文字符号[]"

TK.*_*TK. 11 javascript jslint

我不明白为什么当我使用JavaScript文件运行JSLint时收到错误消息.

os_map = {}; Problem at line 28 character 36: Use the array literal notation [].如果我在JSLint中运行此代码,我会收到消息var .JSLint的选项如下.

/*jslint onevar: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */
Run Code Online (Sandbox Code Playgroud)

声明对象(,它{})应该没问题,但JSLint建议使用空数组(,这是[])

:我找到了答案.我错了.这没什么不对var os_map = {}.代码显示在错误消息中,因为我没有使用"require strict";.我收到错误消息错误.谢谢回答我的问题.

CMS*_*CMS 33

违规行:

var os_autoload_inputs = new Array('searchInput', 'searchInput2',
                                   'powerSearchText', 'searchText');
Run Code Online (Sandbox Code Playgroud)

JSLint不期望看到new Array构造函数,你应该使用[]代替:

var os_autoload_inputs = ['searchInput', 'searchInput2',
                                   'powerSearchText', 'searchText'];
Run Code Online (Sandbox Code Playgroud)

为什么?:

1,克罗克福德不喜欢new.

2,Array可以覆盖对象:

Array = {};
new Array(); // TypeError: Array is not a constructor
Run Code Online (Sandbox Code Playgroud)

3,使用不一致,例如:

var a = new Array(5); // empty 5 elements array
var b = [5]; // 1 element array containing the 5 number on index 0
Run Code Online (Sandbox Code Playgroud)

也可以看看: