473,503 Members | 10,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new Array() vs []

Hello, one is pretending it is /better/ to use new Array() and *never*
use the literal notation [], is he wrong or right ? If he is wrong, as i
suspect he is, how can i explain him, with simple terms :) he is wrong ?

http://javascript.crockford.com/code.html
Here in "Bonus Suggestions" it is write :

"Use {} instead of new Object(). Use [] instead of new Array()."

I've seen it write in a lot of ressources, but never with any concrete
explanation, so why ?

Thanks.

--
laurent
Sep 5 '06 #1
11 15884
Hi Laurent.

Laurent vilday wrote:
Hello, one is pretending it is /better/ to use new Array() and *never*
use the literal notation [], is he wrong or right ? If he is wrong, as i
suspect he is, how can i explain him, with simple terms :) he is wrong ?
Better in what way?

ECMA-262 says (11.1.4):

The production ArrayLiteral: [ Elision(opt) ] is evaluated as follows:

1. Create a new array as if by the expression new Array().
2. Evaluate Elision; if not present, use the numeric value zero.
3. Call the [[Put]] method of Result(1) with arguments "length" and
Result(2).
4. Return Result(1).

And from (15.4.2.1) you can see that new Array() would set "length" to 0
anyway. So they are just the same in terms of behaviour.

[] is shorter to type, and would be quicker to parse (assuming a
reasonable implementation). There's no reason not to use it.

--
Cameron McCormack, http://mcc.id.au/
xmpp:he****@jabber.org â–ª ICQ 26955922 â–ª MSN ca*@mcc.id.au
Sep 5 '06 #2
Cameron McCormack wrote:
[] is shorter to type, and would be quicker to parse (assuming a
reasonable implementation). There's no reason not to use it.
Ancient browsers didn't support it.
I don't even remember which versions of Netscape - 3.0, perhaps? Or even
2.x?
Not using [] and {} may be a recommendation from someone who came to the
conclusion long ago an just hasn't changed it in modern times.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 5 '06 #3
Matt Kruse wrote:
Cameron McCormack wrote:
[] is shorter to type, and would be quicker to parse (assuming a
reasonable implementation). There's no reason not to use it.

Ancient browsers didn't support it.
I don't even remember which versions of Netscape - 3.0, perhaps? Or even
2.x?
Not using [] and {} may be a recommendation from someone who came to the
conclusion long ago an just hasn't changed it in modern times.
<URL: http://jslint.com >

INPUT
var x = new Array();

OUTPUT
Problem at line 1 character 13: Use the array literal notation [].

Sep 5 '06 #4
VK

pe**********@gmail.com wrote:
INPUT
var x = new Array();

OUTPUT
Problem at line 1 character 13: Use the array literal notation [].
JSLint is a great tool with a lot of thoughts put into it, but it has
some highly specific ideas of the proper programming and syntax, so use
with caution :-)

new Array(length) can be used to instantiate an array of a given
initial length without populating it. Unless you have such intention,
squared brackets way could be used.

Yet this issue has relevance to "cool looking vs. lame looking" issue
rather then to the "good programming vs. bad programming" one :-)

Sep 6 '06 #5
JRS: In article <11*********************@m73g2000cwd.googlegroups. com>,
dated Tue, 5 Sep 2006 23:18:12 remote, seen in
news:comp.lang.javascript, VK <sc**********@yahoo.composted :
>new Array(length) can be used to instantiate an array of a given
initial length without populating it. Unless you have such intention,
squared brackets way could be used.

Indeed. But when is it useful to do so?

ISTR there's a related discussion, involving speed, in news:fr.c.l.j -
perhaps a true francophone might report any conclusions?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Sep 6 '06 #6
VK
new Array(length) can be used to instantiate an array of a given
initial length without populating it. Unless you have such intention,
squared brackets way could be used.

Indeed. But when is it useful to do so?
This is the question of a kind "what blue paint can be used for?" You
know for sure that it can be used for many things, but try to give a
sample right away :-)

It can be an array where you do not want to exceed certain length or
where you want to take extra steps when the length exceeds certain
limits.

Say (from my most recent cases) it can be a license limitation for no
more than 10 simultaneous requests to the server. It can be say 1000
records array and then you need to alert user about something (or save,
or refuse to add etc.)
Of course - as almost everything in JavaScript - it can be made in
different ways:

a)
var arr = new Array(1000);
....
if (i arr.length) {
// do something
}
....

b)
var arr = [];
var uBound = 1000;
....
if (i uBound) {
// do something
}
....

c)
var arr = [];
arr.uBound = 1000;
....
if (i arr.uBound) {
// do something
}
....

d) and so on.

But sometimes and rather often the simpliest way is the best one ;-)

ISTR there's a related discussion, involving speed, in news:fr.c.l.j -
perhaps a true francophone might report any conclusions?
JavaScript doesn't allocate/reallocate memory for undefined array
members. So there is no difference between new Array() and [] in
productivity or memory usage. There is nothing to discuss here neither
in French, nor in English - though I read the suggested thread.

Sep 7 '06 #7
ASM
VK a écrit :
>>new Array(length)
[...]
>ISTR there's a related discussion, involving speed, in news:fr.c.l.j -
perhaps a true francophone might report any conclusions?

JavaScript doesn't allocate/reallocate memory for undefined array
members. So there is no difference between new Array() and [] in
productivity or memory usage. There is nothing to discuss here neither
in French, nor in English - though I read the suggested thread.
But probably Firefox doesn't do the job as other browsers ... ?
Answering diffently when you loop a [...] using method for(i in t)

t = new Array();
t[0] = "A";
t[3] = "B";

= IE 5.2 Mac = Safari 1.3.2 = Opera 9 = iCab 3 = Fx 1.5.0.6 =
for(i=0; i<t.length; i++) ==A undefined undefined B
for( i in t ) ==A B
t = ['A',,,'B'];

= IE 5.2 Mac = Safari 1.3.2 = Opera 9 = iCab 3 =
for(i=0; i<t.length; i++) ==A undefined undefined B
for( i in t ) ==A B

= Fx 1.5.0.6 =
for(i=0; i<t.length; i++) ==A undefined undefined B
for( i in t ) ==A undefined undefined B

<http://groups.google.com/group/fr.comp.lang.javascript/msg/3aa5c83edea38f50?rnum=1>

--
Stephane Moriaux et son [moins] vieux Mac
Sep 7 '06 #8
ASM wrote:
t = ['A',,,'B'];

= IE 5.2 Mac = Safari 1.3.2 = Opera 9 = iCab 3 =
for(i=0; i<t.length; i++) ==A undefined undefined B
for( i in t ) ==A B

= Fx 1.5.0.6 =
for(i=0; i<t.length; i++) ==A undefined undefined B
for( i in t ) ==A undefined undefined B
Firefox's behaviour is incorrect here (according to ECMA-262 11.1.4.)

--
Cameron McCormack, http://mcc.id.au/
xmpp:he****@jabber.org â–ª ICQ 26955922 â–ª MSN ca*@mcc.id.au
Sep 8 '06 #9
I never notice this, thank you very much.
Cameron McCormack wrote:
ASM wrote:
t = ['A',,,'B'];

= IE 5.2 Mac = Safari 1.3.2 = Opera 9 = iCab 3 =
for(i=0; i<t.length; i++) ==A undefined undefined B
for( i in t ) ==A B

= Fx 1.5.0.6 =
for(i=0; i<t.length; i++) ==A undefined undefined B
for( i in t ) ==A undefined undefined B

Firefox's behaviour is incorrect here (according to ECMA-262 11.1.4.)

--
Cameron McCormack, http://mcc.id.au/
xmpp:he****@jabber.org â–ª ICQ 26955922 â–ª MSN ca*@mcc.id.au
Sep 8 '06 #10

yu********@gmail.com wrote:
I never notice this, thank you very much.
Please don't top post here.

You probably didn't notice because it is extremely unlikely that anyone
would use an initialiser for a sparse array with an elision large
enough to notice the difference in speed between a for..in and a for
loop. It requires about 400 elements to be measurable at all on my
modest PC, and 10 times that to be noticeable:

var x = [],
i = 400;
while (i--){ x[i] = ',' }
eval('var y = ["A",' + x.join('') + '"B"];');

var startTime = new Date();
for (var x in y){ y[x] }
var endTime = new Date();
alert(y.length + ' elements took '
+ (endTime.getTime()-startTime.getTime()) + 'ms'
);

Cameron McCormack wrote:
ASM wrote:
t = ['A',,,'B'];
>
= IE 5.2 Mac = Safari 1.3.2 = Opera 9 = iCab 3 =
for(i=0; i<t.length; i++) ==A undefined undefined B
for( i in t ) ==A B
>
= Fx 1.5.0.6 =
for(i=0; i<t.length; i++) ==A undefined undefined B
for( i in t ) ==A undefined undefined B
Firefox's behaviour is incorrect here (according to ECMA-262 11.1.4.)
I'd agree with that. No one seems to have logged it as a bug yet, care
to get your name in lights?
--
Rob

Sep 8 '06 #11
RobG wrote:
I'd agree with that. No one seems to have logged it as a bug yet, care
to get your name in lights?
Looking through the SpiderMonkey source, it seems this bug has already
been filed (a couple of years ago):

https://bugzilla.mozilla.org/show_bug.cgi?id=260106

--
Cameron McCormack, http://mcc.id.au/
xmpp:he****@jabber.org â–ª ICQ 26955922 â–ª MSN ca*@mcc.id.au
Sep 8 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2757
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
5155
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
3463
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
55527
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
10198
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
10048
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
16852
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
3168
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.