<body>
<script type="text/javascript; version=1.7">
var fizzbuzz = {
case : [
{
cond : function(n) { return n % 15 == 0; },
proc : function(n) { return "FizzBuzz"; }
},
{
cond : function(n) { return n % 3 == 0; },
proc : function(n) { return "Fizz"; }
},
{
cond : function(n) { return n % 5 == 0; },
proc : function(n) { return "Buzz"; }
},
{
cond : function(n) { return true; },
proc : function(n) { return n + ""; }
}
],
convert : function(n) {
for (var i = 0; i < this.case.length; i++) {
if (this.case[i].cond(n)) {
return this.case[i].proc(n);
}
}
},
get : function(start, count) {
for (var i = start; i < start + count; i++) {
yield this.convert(i);
}
}
}
var ret = "";
for (var x in fizzbuzz.get(1, 100)) {
ret += x + " ";
}
document.body.innerHTML = ret;
</script>
</body>
- It's possible to access fizzbuzz (this) while initializing of fizzbuzz.
- It's possible to use yield keyword in anonymous function.
No comments:
Post a Comment