用for...of遍历对象
//方法一: var obj = { a:1, b:2, c:3 }; obj[Symbol.iterator] = function(){ var keys = Object.keys(this); var count = 0; return { next(){ if(count<keys.length){ return {value: obj[keys[count++]],done:false}; }else{ return {value:undefined,done:true}; } } } }; for(var k of obj){ console.log(k); }
// 方法二 var obj = { a:1, b:2, c:3 }; obj[Symbol.iterator] = function*(){ var keys = Object.keys(obj); for(var k in keys){ yield [k,obj[k]] } }; for(var [k,v] of obj){ console.log(k,v); }