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 *(){for(let p of properties){ yield this[p]; } }; for(let p of user){ console.log( p ); }
时间: 2024-10-12 18:54:26