看到这个题目:
http://blog.zhaojie.me/2010/06/code-for-fun-iterator-generator-yield-in-javascript.html
我也做了一个答案如下:
function range(minInclusive, maxExclusive) { this.next = function(){ return (minInclusive < maxExclusive - 1)? new range(minInclusive + 1, maxExclusive) : null; }; this.value = minInclusive; return this; } for (var iter = range(0, 10); iter; iter = iter.next()) { document.write(iter.value + "<br />"); }