f



a = [ "1", "2", "3" ] v/s a = new Array ( "1", "2", "3" ) identical in all ways?

Do these result in identical objects?

a = [ "1", "2", "3" ]
a = new Array ( "1", "2", "3" )
0
oldyork90 (29)
8/25/2009 12:30:26 PM
comp.lang.javascript 38370 articles. 0 followers. javascript4 (1315) is leader. Post Follow

2 Replies
787 Views

Similar Articles

[PageSpeed] 15

On Aug 25, 1:30=A0pm, okey wrote:
> Do these result in identical objects?
>
> a =3D [ "1", "2", "3" ]
> a =3D new Array ( "1", "2", "3" )

Yes, but:-

a =3D [ 2 ];
a =3D new Array( 2 );

- do not.

Richard.
0
Richard
8/25/2009 12:56:08 PM
okey wrote:
> Do these result in identical objects?
> 
> a = [ "1", "2", "3" ]
> a = new Array ( "1", "2", "3" )

Yes (but not identical when compared with equality operator, of course, 
since those would be two different objects).

However, global `Array` function can be overwritten and due to ambiguity 
in ES3, `new Array` might invoke a function referenced by global `Array` 
(rather than a standard built-in `Array` constructor).

You can observe this in some implementations:

this.Array = function() {
   return { x: 1 };
};

new Array(1,2,3); // { x: 1 }

ES5 specs (currently draft) changed appropriate sections (e.g. 15.4.1.1) 
to avoid ambiguity; `new Array` in ES5 should always result in 
invocation of built-in `Array` constructor.

-- 
kangax
0
kangax
8/25/2009 1:02:46 PM
Reply: