“使用数组文字符号[]” for var os_map = {} [英] "Use the array literal notation []" for var os_map = {}

查看:76
本文介绍了“使用数组文字符号[]” for var os_map = {}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I don't understand why I get the error message when I run JSLint with a JavaScript file.

我收到消息var os_map = {};第28行的问题36:使用数组文字符号[]。如果我运行 JSLint 中的此代码。 JSLint的选项如下所示。

I get the message var os_map = {}; Problem at line 28 character 36: Use the array literal notation []. if I run this code in JSLint. The options for the JSLint as the following.

/*jslint onevar: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */

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

Claiming object (, which is {}) should be okay, but JSLint suggets to use an empty array (, which is [])

:我找到了答案。我错了。 var os_map = {} 没有任何问题。代码显示在错误消息中,因为我没有使用require strict; 。我收到错误消息错误。感谢您回答我的问题。

: I found an answer. I was wrong. There's nothing wrong with var os_map = {}. The code was shown in an error message because I did not use "require strict";. I got the error message wrong. Thanks for answering my questions.

推荐答案

违规行:

var os_autoload_inputs = new Array('searchInput', 'searchInput2',
                                   'powerSearchText', 'searchText');

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

JSLint does not expect to see new Array constructor, you should use [] instead:

var os_autoload_inputs = ['searchInput', 'searchInput2',
                                   'powerSearchText', 'searchText'];

为什么? :

1,Crockford不喜欢 new

1, Crockford doesn't like new.

2,可以覆盖数组对象:

Array = {};
new Array(); // TypeError: Array is not a constructor

3,使用不一致,例如:

3, Usage inconsistencies, e.g.:

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

参见:

  • What’s the difference between "Array()" and "[]" while declaring a JavaScript array?
  • What’s wrong with var x = new Array();

这篇关于“使用数组文字符号[]” for var os_map = {}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆