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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xu, Qian

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

    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?


    --
    best regards
    Xu, Qian (stanleyxu)
  • Peter Michaux

    #2
    Re: Is there any difference between Array() and new Array()

    On Mar 15, 8:55 am, "Xu, Qian" <no_re...@micro soft.comwrote:
    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.o rg/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

    Comment

    • RobG

      #3
      Re: Is there any difference between Array() and new Array()

      On Mar 16, 1:55 am, "Xu, Qian" <no_re...@micro soft.comwrote:
      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]);


      --
      Rob

      Comment

      Working...