zoukankan      html  css  js  c++  java
  • What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

    What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

    问题

    What's the real difference between declaring an array like this:

    var myArray = new Array();
    

    and

    var myArray = [];
    

    回答

    There is a difference, but there is no difference in that example.

    Using the more verbose method: new Array() does have one extra option in the parameters: if you pass a number to the constructor, you will get an array of that length:

    x = new Array(5);
    alert(x.length); // 5
    

    To illustrate the different ways to create an array:

    var a = [],            // these are the same
        b = new Array(),   // a and b are arrays with length 0
    
        c = ['foo', 'bar'],           // these are the same
        d = new Array('foo', 'bar'),  // c and d are arrays with 2 strings
    
        // these are different:
        e = [3]             // e.length == 1, e[0] == 3
        f = new Array(3),   // f.length == 3, f[0] == undefined
    
    ;
    

    Another difference is that when using new Array() you're able to set the size of the array, which affects the stack size. This can be useful if you're getting stack overflows (Performance of Array.push vs Array.unshift) which is what happens when the size of the array exceeds the size of the stack, and it has to be re-created. So there can actually, depending on the use case, be a performance increase when using new Array() because you can prevent the overflow from happening.

    As pointed out in this answer, new Array(5) will not actually add five undefined items to the array. It simply adds space for five items. Be aware that using Array this way makes it difficult to rely on array.length for calculations.

    回答2

    The difference between creating an array with the implicit array and the array constructor is subtle but important.

    When you create an array using

    var a = [];
    

    You're telling the interpreter to create a new runtime array. No extra processing necessary at all. Done.

    If you use:

    var a = new Array();
    

    You're telling the interpreter, I want to call the constructor "Array" and generate an object. It then looks up through your execution context to find the constructor to call, and calls it, creating your array.

    You may think "Well, this doesn't matter at all. They're the same!". Unfortunately you can't guarantee that.

    Take the following example:

    function Array() {
        this.is = 'SPARTA';
    }
    
    var a = new Array();
    var b = [];
    
    alert(a.is);  // => 'SPARTA'
    alert(b.is);  // => undefined
    a.push('Woa'); // => TypeError: a.push is not a function
    b.push('Woa'); // => 1 (OK)
    

    In the above example, the first call will alert 'SPARTA' as you'd expect. The second will not. You will end up seeing undefined. You'll also note that b contains all of the native Array object functions such as push, where the other does not.

    While you may expect this to happen, it just illustrates the fact that [] is not the same as new Array().

    It's probably best to just use [] if you know you just want an array. I also do not suggest going around and redefining Array...

  • 相关阅读:
    HTML、DIV+CSS网页制作中排版混乱的几种常见的情况
    ---------------------------------Javascript零基础到入门
    Bootstrap 框架、插件
    陌陌和请吃饭之类的应用,你要是能玩转,那就厉害了
    冬天去理短发脑门心冷,这时候你需要一顶暖和的棉绒帽子
    我感觉我右手食指要废了,不能双击的赶脚,太伤
    小李子你注定拿不了奥斯卡,谁他么让你长那么帅的
    3月16号的《人生元编程》读者见面会,有人去吗?
    新年要有新气象,额头上留一条杠!
    每日学习笔记12.29.2013
  • 原文地址:https://www.cnblogs.com/chucklu/p/14184688.html
Copyright © 2011-2022 走看看