es6 可迭代对象

var obj = {
    a: 'a',
    b: 'b',
    c: 'c'
}
Object.defineProperty(obj, Symbol.iterator, {
    writeable: false,
    enumerable: false,
    configurable: true,
    value: function () {
        let o = this,
            index = 0,
            keys = Object.keys(o);
        return {
            next: function () {
                return {
                    value: o[keys[index++]],
                    done: index > keys.length
                }
            }
        }
    }
})
for (k of obj) {
    console.log(k);
}

 

posted @ 2020-09-11 23:26  潴哥  阅读(186)  评论(0编辑  收藏  举报