Loading ...
Sorry, an error occurred while loading the content.

372Re: "Use the array literal notation []."

Expand Messages
  • Douglas Crockford
    Jan 30, 2009
      --- In jslint_com@yahoogroups.com, James Clark <sbj@...> wrote:
      > Part of my project uses a statement like the following:
      > var b = new Array(a.length);
      > but jslint complains:
      > Use the array literal notation [].
      > My theory here is that it is better to start the array with the length
      > already initialized, rather than create an empty one and push the
      > elements on later. Depending on how the javascript interpreter is
      > implemented, this might be faster.
      > I'm not exactly sure why using the Array constructor would be bad,
      > except possibly because someone else might have redefined it. So in
      > that case should I be writing this?
      > var b = [].constructor(a.length);
      > Or is my assumption about performance incorrect?

      If you can demonstrate a significant performance advantage, then I
      will relax the test. Stylistically, I think [] is better, but I will
      bow to a compelling performance argument.
    • Show all 5 messages in this topic