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

  • 相关阅读:
    PAIP: Paradigms of Artificial Intelligence Programming
    Common Lisp第三方库介绍 | (R "think-of-lisper" 'Albertlee)
    悲惨世界
    Lisp: Common Lisp, Racket, Clojure, Emacs Lisp
    Github上四种Lisp方言的流行度 | 肉山博客 (Wenshan's Blog)
    Nginx系列~负载均衡服务器与WWW服务器的实现
    Nginx系列~Nginx服务启动不了
    知方可补不足~数据库名称和数据库别名不同了怎么办
    WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递
    WebApi系列~通过HttpClient来调用Web Api接口
  • 原文地址:https://www.cnblogs.com/chucklu/p/14184688.html
Copyright © 2011-2022 走看看