Creating an n-size array where JSLint will be happy?

JSlint does not like the use of Array constructors, and there are no JSLint variants for them. Therefore, to create an array of length n, the following is not allowed:

var arr = new Array(n); 

Is this the only way I can get around this?

 var arr = []; arr.length = 5; 

Under normal circumstances, this does not really matter (using two lines of code instead of one), but I'm sorry that I cannot use multiple line compression:

 function repeat(str, times) { return new Array(times + 1).join(str); } 
+7
source share
1 answer

JSLint is pretty easy to outwit.

You can simply do this:

 function repeat(str, times) { var A = Array; return new A(times + 1).join(str); } 

This will also work:

 function repeat(str, times) { return new Array.prototype.constructor(times + 1).join(str); } 
+2
source

All Articles