Array.length vs Array.prototype.length

前端 未结 4 1928
Happy的楠姐
Happy的楠姐 2020-12-19 15:20

I found that both the Array Object and Array.prototype have the length property. I am confused on using the Array.length

4条回答
  •  自闭症患者
    2020-12-19 15:49

    Array.length provides the number of declared parameters in Array function definition. Since Array function definition has only one size parameter, hence it will return 1 irrespective of your array content.

    Array.prototype.length provides the number of elements in array data. It is dependent on the array content.

    var arr=new Array();
    console.log(Array.length);//returns 1
    console.log(arr.length);//returns 0 as array has 0 elements
    

提交回复
热议问题