let arr = [1,2,3]
for(let val of arr) {
console.log('数组每一项--->',val)
}
let str = '1234'
for (let val of str) {
console.log('字符串每一项-->',val)
}
let arr1 = {
0:'iu',
1:'lisa',
length:2
}
console.log(Array.from(arr1))
let arr2 = Array.from(arr1)
for(let val of arr2) {
console.log(val,'--->类数组')
}
let range = {
from: 1,
to: 5
};
range[Symbol.iterator] = function(){
return {
start:this.from,
end:this.to,
next(){
if(this.start < this.end){
return {done:false,value:this.start++}
}else {
return {done:true}
}
}
}
}
for(let val of range) {
console.log('对象每一项---->',val)
}
let range1 = {
from:1,
to:7,
[Symbol.iterator]:function(){
this.current = this.from
console.log(this,'--->简化')
return this
},
next(){
if(this.current < this.to) {
return {done:false,value:this.current++}
}else {
return {done:true}
}
}
}
for(let val of range1) {
console.log('简化后的3-->',val)
}
console