Modifying .times built-in objects in JavaScript

Never modify JavaScript's standard built-in objects. That's what they [say](https://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice#answer-14034242).

But you're at this hackathon stuck with JS missing Ruby's [.times](https://ruby-doc.org/core-2.2.2/Integer.html#times-method) 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"
```

Nick Ciolpan
29 May 2018
« Back to post