views:

129

answers:

3

I was about to create a trim function in javascript, but as i don't want to reinvent the wheel i googled for this method.
I found this link http://www.somacon.com/p355.php

The Solution it provided is:

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

also it says if you don'y wnt to change the prototype of String then use this:

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/,"");
}

I would like to know in what scenario one should not modify the prototype of String or say any object.

+1  A: 

For this kind of very useful utility function, I'd say you can modify the prototype. But you should be aware that the function may already exist natively in a few browsers, so you should check it : https://developer.mozilla.org/En/Core%5FJavaScript%5F1.5%5FReference/Objects/String

Fabien Ménager
and in which case you should not, is it related to somekind of overhead or what?
Rakesh Juyal
there is no "you shouldn't" case, just be aware of what Fabien said.
Jan Hančič
great javascript 1.8.1 is having string.trim() method https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/String/Trim
Rakesh Juyal
only if i could accept 2 answers, i would have accepted yours too
Rakesh Juyal
+5  A: 

The trim functions are to be standardised in ECMAScript Fifth Edition, as well as already being present in some browsers. So:

  1. Yes, adding them to the prototype is totally appropriate, but

  2. You shouldn't add them to the prototype if they're already there, as you'll just be replacing a fast native-code function with a slow JavaScript one.

It is also typically marginally faster to do trim as two replaces:

// Add ECMA262-5 string trim if not supported natively
//
if (!('trim' in String.prototype)) {
    String.prototype.trim= function() {
        return this.replace(/^\s+/, '').replace(/\s+$/, '');
    };
}
bobince
+1 for `if (!('trim' in String.prototype)) {`
Rakesh Juyal
What's wrong with if (!String.prototype.trim)? Looks more efficient to me.
KooiInc
`in` is more explicit about what it's testing for, not relying on the truthiness of a property or the bizarre and terrible JavaScript quirk of retuning a new `undefined` for something that any other language would consider an exception. For this particular case of course it doesn't matter at all, but in general I always use `in` for testing if an object has a property.
bobince
+1  A: 

In general - do not modify a prototype of buildin objects. But ofcourse you can add your handy function.

And always check before you add:

//pre-1.6 javascript
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(elt) {
        var len = this.length >>> 0;
        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0)
            from += len;
        for (; from < len; from++) {
            if (from in this && this[from] === elt)
                return from;
        }
        return -1;
    };
}

This way you didn't overwrite mush faster buildin function that may become available sometime...

NilColor