How can I extend JavaScript with a global method like .toLowerCase ()?

I am trying to figure out how to extend JavaScript with some of my own methods like toLowerCase ()

Let's say I wanted to use capizeize () or addUnderscores (), whatever it is. I am guessing there is a way to extend or overwrite existing ones.

Also is there a way to do this using jQuery?


source to share

1 answer

Not with jQuery. Just stretch the prototype

specific type you want ...

if (!String.prototype.capitalize) {
    String.prototype.capitalize = function() {
        return this.toUpperCase();

'foobar'.capitalize(); // 'FOOBAR'


The function this

will contain the string you called the method on.