for...of是一个迭代可迭代对象的方式,可迭代对象包括Array、Map、Set、String、TypedArray、arguments 对象等等
语法
for(variable of iterable){ // statement }
for(let a of [1,2,3]){ console.log(a) } // 1 // 2 // 3
for(let s of 'hello'){ console.log(s) } // h // e // l // l // o
for(let s of new Set([1,2,3])){ console.log(s) } // 1 // 2 // 3
Map
for(let s of new Map([[1,1],[2,2]])){ console.log(s) } // (2) [1, 1] // (2) [2, 2]
(function() { for (let argument of arguments) { console.log(argument); } })(1, 2, 3);
for(let p of document.getElementsByTagName('p')){ console.log(p) } // <p>...<p> // <p>...<p> // <p>...<p> // <p>...<p> ...
for...of只能迭代可迭代对象
Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务