Created
May 14, 2016 22:29
-
-
Save sanusart/889e0789dc41478537c72c3c0e66220a to your computer and use it in GitHub Desktop.
Safe(r) String.prototype extend example #js #javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So,
repeat()
is now part of javascript