js for in 和for of的区别
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2021-09-07 10:30:09
参考文档 https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/00143450082788640f82a480be8481a8ce8272951a40970000
语法:
a. for in 便历出来的是属性
b. for of 遍历的是value
c. 手动给对象添加属性后, for in 是可以将新添加的属性遍历出来 但是for of 不行,
d. for in 的属性是使用[] 不可以使用 "." eg: data['index'] instead of data.index
var a = ["A", 'B', 'C']; console.log('for of stat begins'); a.name ='jack'; console.log(a.length); for (var arr_val of a) { console.log(arr_val); } console.log('for in stat begins'); console.log(a.length); for (var arr_val in a) { console.log(a[arr_val]); }