es6 iterator

发布时间 2023-07-05 11:43:19作者: 朝颜陌
// 需求 使用for of 便利对象并返回对象数组的值

let banji = {
    name:"火箭一班",
    stus:[
        "limuzi",
        "nini",
        "zhaoliying",
        "xiena"
    ],
    [Symbol.iterator](){
        let _this = this
        let index = 0
        return {
            next: function(){
                let tmpval = 0
                let tmpDone = 0
                if( index < _this.stus.length ){
                    tmpval = _this.stus[index]
                    tmpDone = false 
                }else{
                    tmpval = undefined
                    tmpDone = true 
                }
                index++
                return { value: tmpval,done:tmpDone}
                
            }
        }
    }
}

for(let v of banji){
    console.log("v:",v)
}