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

后端 未结 18 1481
生来不讨喜
生来不讨喜 2020-11-21 12:00

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

var myArray = new Array();

and

var myArray = [];
<         


        
18条回答
  •  花落未央
    2020-11-21 12:39

    As I know the diference u can find the slice(or the other funcitons of Array) like code1.and code2 show u Array and his instances:

    code1:

    [].slice; // find slice here
    var arr = new Array();
    arr.slice // find slice here
    Array.prototype.slice // find slice here
    

    code2:

    [].__proto__ == Array.prototype; // true
    var arr = new Array();
    arr.__proto__ == Array.prototype; // true
    

    conclusion:

    as u can see [] and new Array() create a new instance of Array.And they all get the prototype functions from Array.prototype

    They are just different instance of Array.so this explain why [] != []

    :)

提交回复
热议问题