Skip to content

Instantly share code, notes, and snippets.

@sanusart
Created May 14, 2016 22:29
Show Gist options
  • Save sanusart/889e0789dc41478537c72c3c0e66220a to your computer and use it in GitHub Desktop.
Save sanusart/889e0789dc41478537c72c3c0e66220a to your computer and use it in GitHub Desktop.
Safe(r) String.prototype extend example #js #javascript
// String.prototype.repeat
// http://jsbin.com/sifesep/edit?js,console,output
Object.defineProperty(String.prototype, 'repeat', {
value: function debug_repeat(times, returnArray) {
var res = [];
var i = 0;
for (i; i < times; i++) {
res.push(this);
}
if (returnArray) {
console.log(res);
return res;
}
console.log(res.join(''));
return res.join('');
},
writable: true,
configurable: true,
enumerable: false
});
// Example:
'Hi'.repeat(3); // -> HiHiHi
'Hi'.repeat(3, true); // -> ["Hi", "Hi", "Hi"]
@sanusart
Copy link
Author

So, repeat() is now part of javascript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment