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

new Array(3)

Expand Messages
  • Douglas Crockford
    JSLint allows new Array(3), but warns on new Array(3, 4) and new Array( 3 ). Someone wrote to me that they were confused by that. He was confused because he
    Message 1 of 8 , Nov 29, 2010
    • 0 Attachment
      JSLint allows new Array(3), but warns on new Array(3, 4) and new Array("3").

      Someone wrote to me that they were confused by that. He was confused because he thought new Array(3) meant [3].

      So, because of that confusion, I think new Array(3) should get a warning as well.

      Comments?
    • Morgaut Alexandre Louis Marc
      Could this be resolved by providing a more explicit warning message on new Array() ? Kind of: new Array() only acceptable to fix the size of a new empty
      Message 2 of 8 , Nov 29, 2010
      • 0 Attachment
        Could this be resolved by providing a more explicit warning message on new Array() ?

        Kind of:
        "new Array() only acceptable to fix the size of a new empty Array. Use the literal notation to provide non-empty arrays"


        On Nov 29, 2010, at 2:15 PM, Douglas Crockford wrote:

        > JSLint allows new Array(3), but warns on new Array(3, 4) and new Array("3").
        >
        > Someone wrote to me that they were confused by that. He was confused because he thought new Array(3) meant [3].
        >
        > So, because of that confusion, I think new Array(3) should get a warning as well.
        >
        > Comments?
        >
        >



        [Non-text portions of this message have been removed]
      • Jordan
        The only uses I can see out of even allowing the new Array syntax at ALL are: 1) allocating memory for an array before you put items into it (not really
        Message 3 of 8 , Nov 29, 2010
        • 0 Attachment
          The only uses I can see out of even allowing the "new Array" syntax at ALL are:
          1) allocating memory for an array before you put items into it (not really applicable to Javascript)
          2) pre-populating an array to a fixed length with `undefined` - basically removing the usefulness of the Array.length property.

          Can anyone provide a use case (one that's actually being used in production code, and not just an academic exercise) in Javascript where using the array literal syntax doesn't provide what you need?

          - Jordan

          --- In jslint_com@yahoogroups.com, Morgaut Alexandre Louis Marc <morgaut@...> wrote:
          >
          > Could this be resolved by providing a more explicit warning message on new Array() ?
          >
          > Kind of:
          > "new Array() only acceptable to fix the size of a new empty Array. Use the literal notation to provide non-empty arrays"
          >
          >
          > On Nov 29, 2010, at 2:15 PM, Douglas Crockford wrote:
          >
          > > JSLint allows new Array(3), but warns on new Array(3, 4) and new Array("3").
          > >
          > > Someone wrote to me that they were confused by that. He was confused because he thought new Array(3) meant [3].
          > >
          > > So, because of that confusion, I think new Array(3) should get a warning as well.
          > >
          > > Comments?
          > >
          > >
          >
          >
          >
          > [Non-text portions of this message have been removed]
          >
        • Marcel Duran
          ... source: http://tech.groups.yahoo.com/group/jslint_com/message/598 [Non-text portions of this message have been removed]
          Message 4 of 8 , Nov 29, 2010
          • 0 Attachment
            >
            >
            > Can anyone provide a use case (one that's actually being used in production
            > code, and not just an academic exercise) in Javascript where using the array
            > literal syntax doesn't provide what you need?
            >
            > new Array(n + 1).join('*'); // string containing n asterisks
            source: http://tech.groups.yahoo.com/group/jslint_com/message/598


            [Non-text portions of this message have been removed]
          • abyssoft@ymail.com
            ... http://tech.groups.yahoo.com/group/jslint_com/message/614 Provides an elegant & effective solution without the use of new Array(n+1) and it operates
            Message 5 of 8 , Nov 29, 2010
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, Marcel Duran <contact@...> wrote:
              >
              > >
              > >
              > > Can anyone provide a use case (one that's actually being used in production
              > > code, and not just an academic exercise) in Javascript where using the array
              > > literal syntax doesn't provide what you need?
              > >
              > > new Array(n + 1).join('*'); // string containing n asterisks
              > source: http://tech.groups.yahoo.com/group/jslint_com/message/598
              >
              >
              > [Non-text portions of this message have been removed]
              >

              http://tech.groups.yahoo.com/group/jslint_com/message/614

              Provides an elegant & effective solution without the use of new Array(n+1) and it operates quickly in all modern browsers.
            • Marcel Duran
              ... a string of n repeated character: http://jsperf.com/string-of-n-character Seems like new Array and [].length are the slowest ones at least in latest
              Message 6 of 8 , Nov 30, 2010
              • 0 Attachment
                >
                >
                > http://tech.groups.yahoo.com/group/jslint_com/message/614
                >
                > Provides an elegant & effective solution without the use of new Array(n+1)
                > and it operates quickly in all modern browsers.
                >
                > I've just made a JSPerf benchmark comparing these various ways of creating
                a string of n repeated character:

                http://jsperf.com/string-of-n-character

                Seems like new Array and [].length are the slowest ones at least in latest
                version of Chrome. Feel free to run this test in different browsers and let
                browserscope take care of benchmark results.

                Marcel


                [Non-text portions of this message have been removed]
              • Morgaut Alexandre Louis Marc
                Very interesting test :-) thanx jsperf ;-) but there was some errors... To be comparable, in those scripts str = a.join( char ); and str = a.join( * ); should
                Message 7 of 8 , Nov 30, 2010
                • 0 Attachment
                  Very interesting test :-)

                  thanx jsperf ;-)

                  but there was some errors...
                  To be comparable, in those scripts
                  str = a.join('char');
                  and
                  str = a.join('*');
                  should be
                  str = a.join(char);



                  Please note also that priority must not be always focused on perf

                  These must also be considered:
                  - Readability of the code
                  - Less code === Less potential error hidden in the pattern (wrong copy/paste or error while typing)

                  So I may be more confident to see these powerful patterns only where they are required
                  (maybe even with a comment to improve the readability)

                  About these two readable solutions, the use of "new Array(n)" vs "a.length = n"
                  I still prefer the first one...

                  here:

                  var tmpArray, myString;

                  tmpArray = [];
                  tmpArray.length = 20;
                  myString = tmpArray.join('*');

                  -> The initialisation of my string variable require to use a temporary variable which will pollute my scope when debugging

                  there

                  var myString;

                  myString = [];
                  myString.length = 20;
                  myString = myString('*');

                  -> myString is first an array (arghhh) before becoming the desired string... and still multiple lines for simple initialisation

                  finally

                  var myString = new Array(10).join('*');

                  -> The initialisation is on one line only and directly provide the good type


                  On Nov 30, 2010, at 11:56 AM, Marcel Duran wrote:

                  > >
                  > >
                  > > http://tech.groups.yahoo.com/group/jslint_com/message/614
                  > >
                  > > Provides an elegant & effective solution without the use of new Array(n+1)
                  > > and it operates quickly in all modern browsers.
                  > >
                  > > I've just made a JSPerf benchmark comparing these various ways of creating
                  > a string of n repeated character:
                  >
                  > http://jsperf.com/string-of-n-character
                  >
                  > Seems like new Array and [].length are the slowest ones at least in latest
                  > version of Chrome. Feel free to run this test in different browsers and let
                  > browserscope take care of benchmark results.
                  >
                  > Marcel
                  >
                  > [Non-text portions of this message have been removed]
                  >
                  >



                  [Non-text portions of this message have been removed]
                • Douglas Crockford
                  ... Readability is paramount. So the inherent off-by-one problem in the trick makes it hazardous. Certainly it does not justify keeping new Array in the safe
                  Message 8 of 8 , Nov 30, 2010
                  • 0 Attachment
                    --- In jslint_com@yahoogroups.com, Morgaut Alexandre Louis Marc <morgaut@...> wrote:
                    > Please note also that priority must not be always focused on perf
                    >
                    > These must also be considered:
                    > - Readability of the code
                    > - Less code === Less potential error hidden in the pattern (wrong copy/paste or error while typing)

                    Readability is paramount. So the inherent off-by-one problem in the trick makes it hazardous. Certainly it does not justify keeping new Array in the safe subset. The fact that new Array(3) is not the same as [3] is a more serious problem than the need to make a string with the wrong number of stars in it.

                    Is there another essential use for new Array?
                  Your message has been successfully submitted and would be delivered to recipients shortly.