diceline-chartmagnifiermouse-upquestion-marktwitter-whiteTwitter_Logo_Blue

Today I Learned

Modifying .times built-in objects in JavaScript

Never modify JavaScript's standard built-in objects. That's what they say.

But you're at this hackathon stuck with JS missing Ruby's .times iterator and that's what JS's prototypes are for after all.

Here's how you do it:

String.prototype.times = function (n) {
    for (var i = 0; i < parseInt(this); i++) {
        n();
    }
}
var myFunc = function () { console.log('Hello World') }
"2".times(myFunc)

Output:

"Hello World"
"Hello World"