What are the repercussions of adding functions to the String class in JavaScript? Is this a bad idea? E.g.,
// String functions
String.prototype.startsWith = function(string) {
return (this.indexOf(string) === 0);
}
String.prototype.empty = function() {
//console.log($.trim(this.valueOf()));
if($.trim(this.valueOf()) == '') {
return true;
}
else {
return false;
}
}
Performance-wise, there is zero effect.
But it's still not a great idea, imo. You're now depending on globally altered state. What happens if some other module does the same thing? Or worse, does a slightly different thing, but using the same name? Ouch.
Better to just define functions that take String arguments, and then use the functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With