iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 3
2
自我挑戰組

JavaScript Array x 學習筆記系列 第 3

[Day 03 | Array.prototype.length]

今天介紹陣列的屬性 - length

length 是陣列的屬性之一,

主要的功能可以用來判斷字串的長度,

其用法也相當簡單,

只要在自己定義的陣列後面加上.length 即可


Syntax

Array.length

Example

  • 範例一

  • 範例二

程式碼如下:

const House = ['Amy\'s house', 'Gin\'s house', 'Lion\'s house', 'Cat\'s house'];

console.log('House.length is:' + House.length);
console.log('House[2] is :' + House[2])


const classmate = new Array('David','Jerry','Andy' ,'Hansen' ,'Sunny' , 'Liam');

console.log('classmate.length is:' + classmate.length);
console.log('classmate[3] is :' + classmate[3]);

下大雨的天....一到辦公室就濕答答...


上一篇
[Day 02 | How to declare an array ? ]
下一篇
[Day 04 | Array.prototype.push () ]
系列文
JavaScript Array x 學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

1
fillano
iT邦超人 1 級 ‧ 2020-09-15 13:46:24

嚴格說起來,你標題用Array.length跟你要表達的東西是不一樣的(之後的標題都一樣有這個問題),以Array.length來說,結果應該永遠是1,因為這是Array當作constructor函數所定義的參數個數。

如果是instance使用的屬性或方法,建議加上prototype,例如Array.prototype.length。MDN上面就是這樣用。

另外,Array有靜態方法,例如Array.isArray(),不加上prototype就很難區別了。

Hoie iT邦新手 5 級 ‧ 2020-09-15 14:12:18 檢舉

!! 意外釣出大師 !!
之後文章會加上 prototype
謝謝大師指點
歡迎常來 XDD

我要留言

立即登入留言