7

JSlint doesn't like the use of Array constructors and there are no JSLint options for allowing them. Therefore, to create an Array of length n, the following is not allowed:

var arr = new Array(n);

Is the below the only way I can get around this?

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

In normal circumstances this is not a big deal (using two lines of code instead of one), but I regret not being able to use the concise string multiplier hack:

function repeat(str, times) {
    return new Array(times + 1).join(str);
}
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
  • possible duplicate of [Proper way to initialize an array's length in javascript?](http://stackoverflow.com/questions/4852017/proper-way-to-initialize-an-arrays-length-in-javascript) – Michael Berkowski Mar 09 '12 at 03:51
  • 1
    Why do you need to create an n-sized array? Given you've described the string multiplier function as using a "hack" I'm surprised you care what jslint says about `new Array()`... but you can easily implement the `repeat()` function with a for loop or the `.length =` thing if that's the only reason you need `new Array()`. – nnnnnn Mar 09 '12 at 03:52
  • *"I regret not being able to use..."* Are you under some constraint that requires you use JSLint? –  Mar 09 '12 at 04:03
  • @Michael No, it's not a duplicate. – Ates Goral Mar 09 '12 at 04:15
  • 1
    @nnnnnn This is about having to use 3 lines of code when a clever one-liner is available... Have you read the use case in my question? – Ates Goral Mar 09 '12 at 04:17
  • @amnotiam I'm pushing the use of JSLint to enforce code consistency within my team. – Ates Goral Mar 09 '12 at 04:18
  • Given you've encapsulated the clever one-liner in a function anyway I don't see that it matters at all if you have to change it to three lines of code. Clever for the sake of clever usually reduces readability and therefore reduces maintainability. Though having said that, what's wrong with ignoring the JSLint warning in this case? It's not automatically right about _everything,_ no matter what Mr Crockford might think... – nnnnnn Mar 09 '12 at 04:43
  • @nnnnnn Well, I already gave the disclaimer, "In normal circumstances this is not a big deal". This was a curiosity-driven question, waiving concepts like readability and maintainability. SO would be a boring place if we constrained questions to only the ones that make business-sense ;) And, no I can't ignore the JSLint error (it's not a warning). The JSlint Maven plug-in stops the build in its tracks. – Ates Goral Mar 09 '12 at 05:09

1 Answers1

2

JSLint is fairly easy to outsmart.

You can just do this:

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

This would also work:

function repeat(str, times) {
    return new Array.prototype.constructor(times + 1).join(str);
}
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • 3
    No problem. The interesting thing to note is that to work around the linter, you've actually produced more awkward code, so the linter is doing the opposite of what it's intended for. If you run into this a lot, you might consider looking into (or having the project lead look into) other linters. – Dagg Nabbit Mar 09 '12 at 04:21
  • I really don't mind the awkwardness. I was genuinely interested in seeing if there was a hack to make JSLint ignore the hack. – Ates Goral Mar 09 '12 at 05:11
  • This one's easy to work around, but wait until you try to do assignment in a loop condition... I find JSLint to be needlessly restrictive. You'll never be able to do this: `var node = document.body.firstChild; do { console.log(node); } while (node = node.nextSibling);` – Dagg Nabbit Mar 09 '12 at 05:19