iT邦幫忙

2021 iThome 鐵人賽

DAY 13
0
Modern Web

YDKJS 一邊讀 You Don't Know JS Yet 一邊卡關一邊弄懂的日子 ԅ(≖‿≖ԅ)系列 第 13

Day13 - this&Object Prototypes Ch3 Objects - Contents - Existence - Enumeration 作者建議

使用 in 搭配 for 時, array 內所有 enumerable 為 true 的 property 都會被抓出來,所以作者建議 in 搭配 for 的用法用在 object 就好

var myArr = ['a','b','c']; 

// property 的預設 enumerable 是 true
myArr['food'] = '燃麵' 

console.log(myArr) // ["a", "b", "c"]
console.log(1 in myArr); // true
console.log(myArr.hasOwnProperty( 1 )); // true

// 作者建議 for 迴圈用在 array 身上,還是以這種傳統方式
for (var i=0; i<myArr.length;i++) {
	console.log( myArr[i] );
} // 'a','b','c'

// 因為使用 in 搭配 for 時, array 內所有 enumerable 為 true 的 property 都會被抓出來
for (item in myArr) {
	console.log( item );
} // "0","1","2","food"

今天筆記到這,如果內容有出入,都在麻煩糾正了,謝謝您 ԅ(≖‿≖ԅ)


上一篇
Day12 - this&Object Prototypes Ch3 Objects - Contents - Existence - Enumeration
下一篇
Day14 - this&Object Prototypes Ch3 Objects - Contents - Existence - Enumeration 作者建議
系列文
YDKJS 一邊讀 You Don't Know JS Yet 一邊卡關一邊弄懂的日子 ԅ(≖‿≖ԅ)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言