Is there any difference between Array() and new Array()

X

Xu, Qian

Hi All,

I have defined a function
function fct(arr) { ... }

When I call this function, I prefer to write
fct(Array(item1, item2));

But if I write
fct(new Array(item1, item2));
The code above works as well. But Will it cause any memory leaks?
 
P

Peter Michaux

Hi All,

I have defined a function
function fct(arr) { ... }

When I call this function, I prefer to write
fct(Array(item1, item2));

But if I write
fct(new Array(item1, item2));
The code above works as well. But Will it cause any memory leaks?

--------------

<URL: http://www.ecma-international.org/publications/standards/Ecma-262.htm>

15.4.1 The Array Constructor Called as a Function
When Array is called as a function rather than as a constructor, it
creates and initialises a new Array
object. Thus the function call Array(...) is equivalent to the object
creation expression
new Array(...) with the same arguments.

--------------

Of course, you need to test the implementations to see if they
conform. I would guess "new Array()" is safer than "Array()".

The advantage of "new Array()" looks the same as your own JavaScript
constructors "new MyConstructor()"

Peter
 
R

RobG

Hi All,

I have defined a function
   function fct(arr) { ... }

When I call this function, I prefer to write
   fct(Array(item1, item2));

But if I write
   fct(new Array(item1, item2));
The code above works as well.

Further to what Peter wrote, you could also use an array literal:

fct([item1, item2]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top