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.

[source:javascript]

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

[/source]

Easy.

Special note : I don't want you to copy/paste that code. It's for the purpose of the demonstration only.

I can do the same thing for every objects wheter they are intrinsic javascript classes, third-party classes or my own classes.

comments powered by Disqus