“Use the array literal notation []” for var os_map = {}

跟風遠走 提交于 2019-11-30 12:08:57
CMS

The offending line:

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

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

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

Why? :

1, Crockford doesn't like new.

2, The Array object could be overridden:

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

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

See also:

Change this:

var a = new Array(5);

for this:

var a = new [].constructor(5);

My read of line 28 in that code is:

var os_autoload_forms = new Array('searchform', 'searchform2', 'powersearch', 'search' );

...where it would indeed be appropriate to use the literal array syntax:

var os_autoload_forms = ['searchform', 'searchform2', 'powersearch', 'search' ];

It should be saying the same thing about line 27, though.

Problem at line 16 character 32: Use the array literal notation []. if I run this code in JSLint. The options for the JSLint as the following.

just do

var arrayName= [];

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!