javascript - "Use the array literal notation []"对于 var os_map = {}

标签 javascript jslint

我不明白为什么在使用 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/

相关文章:

javascript - 如果文件中标记为全局函数,jslint 不会查看它们吗?

twitter-bootstrap - 在构建 Twitter Bootstrap 时让 jshint 忽略某些文件

javascript - 比较操作 - 正确的语法?

javascript - JSLint 将 'window' 作为全局变量的问题

javascript - 如何在使用 require.context 后动态加载 Vue 组件?

javascript - Angular JS : How to avoid loss of data of $http call on page refresh?

javascript - 如何使用javascript更改html中标签的属性

javascript - 用于 Javascript 的 PyFlakes?

javascript - 移动时可选择的单选按钮

javascript - 如何显示和隐藏内部 div onclick 最多 3 个级别?