[ES6] Generators

Example 1:

function *topicList(){
  yield "ES2015";
  yield "Semi-colons: good or bad?";
  yield "TypeScript";
}

for( let topic of topicList() ){
  console.log( topic );
}

Example2:

let user = {
  name: "sam", totalReplies: 17, isBlocked: false
};

user[Symbol.iterator] = function  *(){

let properties = Object.keys(user);

for(let p of properties){ yield this[p]; } }; for(let p of user){ console.log( p ); }
原文地址:https://www.cnblogs.com/Answer1215/p/5131665.html