使用for..of输出object

        let obj = {
            time: "8.8",
            doing: "listen to music",
            who: "sbluo",
            length: 3,
            [Symbol.iterator]() {
                let _this = this;
                let index = 0;
                let arr = [];
                for (let key in obj) {
                    arr.push(key)
                }
                return {
                    next() {
                        if (index < _this.length) {
                            return {
                                value: _this[arr[index++]],
                                done: false
                            }
                        }
                        else {
                            return {
                                value: undefined,
                                done: true
                            }
                        }
                    }
                }
            }
        }
        for (let val of obj) {
            console.log(val);
        }

posted @ 2021-10-25 19:03  yongerbingxuan  阅读(41)  评论(0)    收藏  举报