11

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

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 */

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

: 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.

Community
  • 1
  • 1
TK.
  • 23,367
  • 19
  • 60
  • 72

4 Answers4

33

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:

Community
  • 1
  • 1
Christian C. Salvadó
  • 723,813
  • 173
  • 899
  • 828
3

Change this:

var a = new Array(5);

for this:

var a = new [].constructor(5);
Eduardo Cuomo
  • 13,985
  • 3
  • 93
  • 80
1

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.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Thanks. You are right. I mistook the error message. Please read the "Update" in my question. – TK. Dec 20 '09 at 15:17
0

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= [];

Rahul Shinde
  • 1,205
  • 1
  • 11
  • 13