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...

  • 相关阅读:
    SQLServer 错误: 15404,维护计划无法执行
    Axis2 服务器端抛出ServiceClass object does not implement问题解决方法
    领域驱动设计 软件核心复杂性应对之道 读书笔记
    华为实施微服务架构的五大军规
    DDD领域驱动设计基本理论知识总结
    TransactionScope使用说明
    错误:该行已经属于另一个表
    SQL基础之 时间戳
    采用左右值编码来存储无限分级树形结构的数据库表设计
    违反并发性: UpdateCommand影响了预期 1 条记录中的 0 条 解决办法
  • 原文地址:https://www.cnblogs.com/chucklu/p/14184688.html
Copyright © 2011-2022 走看看