//自定义遍历器
class RangeIterator {
constructor(start, stop) {
this.value = start;
this.stop = stop;
}
[Symbol.iterator]() {
return this;
}
next() {
var value = this.value;
if (value < this.stop) {
this.value++;
return { done: false, value: value };
}
return { done: true, value: undefined };
}
}
var range = new RangeIterator(0, 3);
for (var value of range) {
console.log(value);
}
//遍历字符串
for (let s of "ECMAScript") {
console.log(s);
}
//类数组对象
let obj = { 0: 'zhangsan', 1: 'lisi', 2: 'wangwu', length: 3 };
let arrObj = Array.from(obj);
for (let v of arrObj) {
console.log(v);
}
//遍历普通对象
let person = { Name: "zhangsan", Age: 19, Height: 180 };
for (let key in person) {
console.log(key);
}
for (let key of Object.keys(person)) {
console.log(key + '->' + person[key]);
}
console