Nov12
Number.prototype
Monkey-patching the Number global in JavaScript seems innocuous, and usually, that would be correct. I mean, who actually uses methods on Number instances, right? Your only worry is that someone else had the same idea in a another library, with different semantics. Other than that, you're fine. Anyway, this is my favorite Number trick:
Number.prototype.times = function(funct){
if(typeof funct === 'function') {
for(var i = 0;i < Math.floor(this);i++) {
funct(i);
}
}
return this;
}
(20).times(function(){
print("You're a jerk.");
});
Reads better than a for loop.
Bonus. Repeat a string 20 times:
new Array(21).join("I know.");