es6基础0x015:for...of

发布时间:2019-08-09 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了es6基础0x015:for...of脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

0x000 概述

for...of是一个迭代可迭代对象的方式,可迭代对象包括ArrayMapSetStringTyPEdArrayarguments 对象等等

0x001 语法

for(VARiable of ITerable){
    // statement
}

0x001 迭代数组

for(let a of [1,2,3]){
    console.LOG(a)
} 
// 1
// 2
// 3

0x002 迭代字符串

for(let s of 'hello'){
    console.log(s)
}
// h
// e
// l
// l
// o

0x003 迭代Set

for(let s of new Set([1,2,3])){
    console.log(s)
}
// 1
// 2
// 3

0x004 迭代Map

for(let s of new Map([[1,1],[2,2]])){
    console.log(s)
}
// (2) [1, 1]
// (2) [2, 2]

0X005 迭代arguments

(function() {
  for (let argument of arguments) {
    console.log(argument);
  }
})(1, 2, 3);

0x006 迭代Dom集合

for(let p of document.getElementsByTagName('p')){
    console.log(p)
}
// <p>...<p>
// <p>...<p>
// <p>...<p>
// <p>...<p>
...

0x007 总结

for...of只能迭代可迭代对象

脚本宝典总结

以上是脚本宝典为你收集整理的es6基础0x015:for...of全部内容,希望文章能够帮你解决es6基础0x015:for...of所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。