1.8 KiB
1.8 KiB
title | localeTitle |
---|---|
For...Of Loop | 对于...... Of Loop |
for...of
语句创建循环遍历可迭代对象(包括Array,Map,Set,Arguments对象等),调用自定义迭代挂钩,其中包含要为每个不同属性的值执行的语句。
for (variable of object) {
statement
}
| |说明| | ---------- | ------------------------------------- | |变量|在每次迭代时,将不同属性的值分配给变量。 | |对象|迭代其可枚举属性的对象。 |
例子
排列
let arr = [ "fred", "tom", "bob" ];
for (let i of arr) {
console.log(i);
}
// Output:
// fred
// tom
// bob
地图
var m = new Map();
m.set(1, "black");
m.set(2, "red");
for (var n of m) {
console.log(n);
}
// Output:
// 1,black
// 2,red
组
var s = new Set();
s.add(1);
s.add("red");
for (var n of s) {
console.log(n);
}
// Output:
// 1
// red
参数对象
// your browser must support for..of loop
// and let-scoped variables in for loops
function displayArgumentsObject() {
for (let n of arguments) {
console.log(n);
}
}
displayArgumentsObject(1, 'red');
// Output:
// 1
// red