www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Declaring Arrays with constructors or just []

  1. #1
    Join Date
    Dec 2008
    Posts
    488

    Question Declaring Arrays with constructors or just []

    Strictly speaking, is it more correct to declare an array using the constructor:
    Code:
    var myArray = new Array();
    or is it acceptable by standards to simply declare an array using an empty subset:
    Code:
    var myArray = [];
    JSLint accepts both methods, but it won't read run-time "strict" errors or warnings. It's very difficult to find strict JavaScript information

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    They are identical. The shorthand notation is more concise.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Dec 2008
    Posts
    488
    So, both are accepted as standard?

  4. #4
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    The only difference is you can't declare the initial length of an array using short syntax, i.e. there's no equivalent for

    Code:
    var foo = new Array(5); //start with 5 empty keys

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by jamesbcox1980 View Post
    So, both are accepted as standard?
    Yeap. The second one [] is linked with JSON: http://json.org

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    var x=new Array(), y=new Array, z=[];
    x==y==z==true


    NOTE: you don't need to use new with Arrays...
    Last edited by rnd me; 12-03-2009 at 02:35 PM.

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by mitya View Post
    The only difference is you can't declare the initial length of an array using short syntax, i.e. there's no equivalent for

    Code:
    var foo = new Array(5); //start with 5 empty keys
    i beg to differ:
    Code:
    x=[,,,,,];

    But i'll gladly concede the point for Array(99999)....

  8. #8
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    But i'll gladly concede the point for Array(99999)....
    Lol (oops, sorry..).
    Length declaration? We don't need no stinking length declarations....

  9. #9
    Join Date
    Dec 2008
    Posts
    488
    Quote Originally Posted by rnd me View Post
    i beg to differ:
    Code:
    x=[,,,,,];

    But i'll gladly concede the point for Array(99999)....
    Well that's cool I didn't think about it that way! Thanks. Also, I was just reading JSLint's documentation, and it looks like it prefers [].

  10. #10
    Join Date
    Dec 2008
    Posts
    488
    Quote Originally Posted by astupidname View Post
    Lol (oops, sorry..).
    Length declaration? We don't need no stinking length declarations....
    Length declarations help with memory management on the C side though don't they?

  11. #11
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Length declarations help with memory management on the C side though don't they?
    I personally have not a great deal of knowledge of C, have studied C++ a bit and do know a little (very little) about the requirements there somewhat for length declarations. It is to the best of my knowledge not an issue in javascript, I presume the compiler recreates and garbage collects upon every change to array in javascript, but I could be a mile off on that, unsure. All I know is that it is not required to use length declarations, and even advised against (total waste of code and requires more memory usage to invoke via 'new' keyword -unless you go rnd_me's way! ha ha)) to some extent, in javascript.

  12. #12
    Join Date
    Dec 2008
    Posts
    488
    Quote Originally Posted by astupidname View Post
    I personally have not a great deal of knowledge of C, have studied C++ a bit and do know a little (very little) about the requirements there somewhat for length declarations.
    Sorry, that's what I meant-- C++.

    Cool, thanks for the info everyone!

  13. #13
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by astupidname View Post
    I personally have not a great deal of knowledge of C, have studied C++ a bit and do know a little (very little) about the requirements there somewhat for length declarations

    ....

    advised against (total waste of code and requires more memory usage to invoke via 'new' keyword -unless you go rnd_me's way! ha ha)) to some extent, in javascript.
    pre-sizing offers no memory advantages. a numeric argument to Array simply sets the .length property for the purposes of methods.
    An array is basically a generic object with a custom toString, .length, and [SET] method, where numeric indeces are bundled in the toString/valueOf methods.
    This is how the [SET] method differs from say, the String() constructor, where using "new" (or not) makes a diff...

    ....

    Omitting "new" does nothing; only on custom generics does omitting new prevent the apropos prototype binding.
    With Arrays, you always have array.pop, and all other normal protos for that matter.
    Last edited by rnd me; 12-03-2009 at 04:13 PM.

  14. #14
    Join Date
    Dec 2008
    Posts
    488
    Ah, ok. See, the purpose in setting the array length is that in other languages, the array size cannot be automatically increased. But JS does this automatically. I just found another good reason for always using brackets: http://www.hunlock.com/blogs/Masteri...rays#quickIDX1

    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.

    Omitting "new" does nothing; only on custom generics does omitting new prevent the apropos prototype binding.
    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

  15. #15
    Join Date
    Dec 2008
    Posts
    488
    Also, Kor mentioned linking the array with JSON when using []. What exactly does that mean?

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



Recent Articles