In Ruby, you can do this:
3.times { print "Ho! " } # => Ho! Ho! Ho!
I tried to do it in JavaScript:
Number.prototype.times = function(fn) {
for (var i = 0; i < this; i++) {
fn();
}
}
This works:
(3).times(function() { console.log("hi"); });
This doesn't
3.times(function() { console.log("hi"); });
Chrome gives me a syntax error: "Unexpected token ILLEGAL". Why?