Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18

Thread: Declaring Arrays with constructors or just []

  1. #16
    Join Date
    Dec 2008
    Oh, I think I see... you mean it's just the format used in JSON.

  2. #17
    Join Date
    Jul 2008
    urbana, il
    Quote Originally Posted by jamesbcox1980 View Post

    So my conclusion is that using brackets is generally more correct than using the "new" constructor. There might be some proprietary instances where you need to initialize an empty, sized array using the value or property of something else and loop through it or something, but for the most part the "new" constructor is not needed.

    I had a discussion about this with someone a while back, and they claimed it DID in fact produce a lot of prototype overhead. I'm not sure which to go with, considering that person's advanced knowledge of JS.

    Another question, when considering prototypes and other object bindings, are arrays treated the same as HTML collections or object collections? Like getElementsByTagName or an object array containing date objects
    i'd like to think my knowledge of JS is advanced, but i'll let others decide.
    As i said, new does nothing on Array, but "the other guy" was right for 99% of constructors: new will add additional prototype members that may not be needed. The IS a diff between x=String(555) and x=new String(555); one yields a primitive, the later yields an object.
    Often, you can simply omit "new", but sometimes it's inconstant, especially with implantation-specific constructors.

    For example, x=Option("red") is fine in FF, but won't work in IE...

    Arrays are NOT treated the same as HTML collections.
    HTML collections are generic (internal technically) objects that have a length. ".length" is "magical" in javascript objects, and it's inclusion has an effect on all objects, not just collections.

    If an object has a .length member, you can iterate all the objects' integer keys from 0- .length with a for loop.

    Strings are a good example of this, but consider also the following:
    var fakeArray={0:"fred", 1:"bob", 2:"sally", 3:"jane", length:4};
    for(var i=0;i<fakeArray.length;i++){
    .length really is magic:
    var fakeArray={0:"fred", 1:"bob", 2:"sally", 3:"jane", length:4};
    for(var i=0;i<fakeArray.length;i++){
      alert(fakeArray[i]);//fred,bob(in order, 1 at a time)
    //note diff from Array: (not discarding props under .length):
    alert(fakeArray.toSource()) //({0:"fred", 1:"bob", 2:"sally", 3:"jane", length:2})
    Last edited by rnd me; 12-03-2009 at 07:06 PM.
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  3. #18
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    Quote Originally Posted by rnd me
    Arrays are NOT treated the same as HTML collections.
    Agree, but I would rather say, in reverse, that the HTML collections (the so called Node Lists) are the one which must not be treated the same as the arrays. They are NOT arrays. They don't support some of the array's method, such as push(), slice(), reverse()...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center