好好爱自己!

Array.length vs Array.prototype.length

I found that both the Array Object and Array.prototype have the length property. I am confused on using the Array.length property. How do you use it?

 

Answer:

Array is a constructor function.

All functions have a length property that returns the number of declared parameters in the function definition.

Array.length is how many arguments the function Array() takes and Array.prototype.length is an instance method that gives you the length of your array. When you check ['foo'].length you're actually checking Array.prototype.length with the this argument being your array ['foo']

var myArray = ['a','b','c']
console.log(myArray.length); //logs 3

  

 

posted @ 2016-11-16 13:36  立志做一个好的程序员  阅读(307)  评论(0编辑  收藏  举报

不断学习创作,与自己快乐相处