Extend javascript classes


The intrinsec objects of javascript (String, Number, Date, etc) are missing a lot of handy methods. God knows why. Example, you don’t have a trim() function on a String object. Maybe the developers thought that it was easy enough to write theString.replace(/^\s*|\s*$/g, “”) to trim a string but that’s not the kind of ugly code I want to see everywhere in my projects. It’s unesthetical. To do this, I have to use prototype.
So I want to add a trim() method to all my objects that are String class.

String.prototype.trim = function()
{
return this.replace(/^\s*|\s*$/g, “”);
}

0 comments: