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

2977Re: [jslint] Re: Array construction bug

Expand Messages
  • Arild Haugstad
    Aug 31, 2012
    • 0 Attachment
      When experimenting with the developer console in Chrome and FireBug in
      FireFox, the effects of the two methods appear to be *exactly* the
      same.
      While setting the "length" property of an Array is not very useful;
      constructing the array with a numeric arguments gives the same result;
      i.e.
      var myArray = new Array(50);
      results in an array with no actual elements and "length" set to 50, while
      var myArray = [];
      myArray.length = 50;
      also results in an array with no actual elements and "length" set to 50...

      Do other browser actually behave differently in a way which makes the
      first variant more useful than the last?



      On 31 August 2012 10:22, Joe Hansche <madcoder@...> wrote:
      > According to MDN that is not what actually happens:
      > https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/length
      >
      > You can only truncate an array's length. Artificially increasing an array's
      > "length" still serves no purpose if you don't initialize what's *in* it.
      > On Aug 31, 2012 3:59 AM, "Alexandre Morgaut" <morgaut@...> wrote:
      >
      >> **
      >>
      >>
      >> Yes there is a workaround, one that still respect JSLint guidelines
      >>
      >> You can fix the length without the array constructor, even with a dynamic
      >> value
      >>
      >> Just do:
      >>
      >> var len = 50;
      >> var arr = [];
      >> arr.length = len;
      >>
      >> --- In jslint_com@yahoogroups.com, Martin Cooper <mfncooper@...> wrote:
      >> >
      >> > If I need to create an array with a specified length, and the length
      >> > is a literal, JSLint is happy enough with this:
      >> >
      >> > var arr = new Array(50);
      >> >
      >> > If, however, the value comes from a variable:
      >> >
      >> > var len = 50,
      >> > arr = new Array(len);
      >> >
      >> > JSLint complains with an unusable "solution", despite the value being a
      >> Number:
      >> >
      >> > Use the array literal notation [].
      >> >
      >> > Of course, if I use array literal notation, I don't get the semantics I
      >> want.
      >> >
      >> > Seems like a bug to me. Is there a workaround?
      >> >
      >>
      >>
      >>
      >
      >
      > [Non-text portions of this message have been removed]
      >
      >
      >
      > ------------------------------------
      >
      > Yahoo! Groups Links
      >
      >
      >
    • Show all 21 messages in this topic