Since Javascript 1.7 there is an Iterator object, which allows this:(从Javascript 1.7开始,有一个Iterator对象,允许这样:)
var a={a:1,b:2,c:3};
var it=Iterator(a);
function iterate(){
try {
console.log(it.next());
setTimeout(iterate,1000);
}catch (err if err instanceof StopIteration) {
console.log("End of record.
");
} catch (err) {
console.log("Unknown error: " + err.description + "
");
}
}
iterate();
is there something like this in node.js ?(在node.js中有这样的东西吗?)
Right now i'm using:(现在我正在使用:)
function Iterator(o){
/*var k=[];
for(var i in o){
k.push(i);
}*/
var k=Object.keys(o);
return {
next:function(){
return k.shift();
}
};
}
but that produces a lot of overhead by storing all the object keys in k
.(但是通过将所有对象键存储在k
中会产生大量开销。)
ask by stewe translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…