1. 程式人生 > 其它 >es6 迭代器-symbol.iterator

es6 迭代器-symbol.iterator

迭代器-symbol.iterator

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>迭代器-symbol.iterator</title>

</head>
<body>
<div id="ad">

</div>
<div></div>
<script>

    const school = {
        name: "清華大學",
        student:[
            "小明",
            "小剛"
        ],
        [Symbol.iterator](){
            let _this = this;
            let index = 0;
            return {
                next(){
                    if(index < _this.student.length){
                        const result = {value:_this.student[index],done:false};
                        index++;
                        return result;
                    }else{
                        return {value:undefined, done:true}
                    }
                }
            }
        }
    };

    console.log('test');
    for(let i of school){
        console.log(i);
    }
</script>
</body>
</html>