0

Was speaking with a developer today and he said it was possible to operate on a string literal like this using prototype. Can anyone explain how that might be possible?

var myVar = "some string literal".reverse(); 
alert(myVar); 

and i'd need to implement some reverse method to receive the alerted output ("literal string some");

(not looking for the algorithm, just how i can operate on a string literal like this)

Brad
  • 6,106
  • 4
  • 31
  • 43

2 Answers2

4

You can make your own new method that would return a new string with the characters reversed.

String.prototype.reverse = function() {
    return this.split("").reverse().join("");
}

Working example: http://jsfiddle.net/jfriend00/jXA2x/

FYI, it is generally not a great idea to extend built-in types like this as it can cause problems in some circumstances, particularly when trying to interoperate with other pieces of code.

If you want to split and reverse on word boundaries or some other criteria, then you can adjust the argument passed to .split() according to whatever logic you want to use and then modify .join() accordingly to match:

String.prototype.reverse = function() {
    return this.split(/\b/).reverse().join(" ");
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Not sure the question is relevant to immutability at all – zerkms Apr 24 '13 at 04:59
  • 1
    PS: also it might be a good idea to mention that extending of base classes is a bad practice. – zerkms Apr 24 '13 at 05:00
  • 1
    the original question says "and I'd need to implement some reverse method to receive the alerted output `'literal string some'`", it's reversal over spaces, not across all characters. – zzzzBov Apr 24 '13 at 05:00
  • @zzzzBov - what do you think that phrase in the question means? I have no idea. – jfriend00 Apr 24 '13 at 05:02
  • 1
    the original string was `some string literal`, and OP wants `reverse` to produce `literal some string`, meaning that it's reversing the order of the words, but not the order of the letters. – zzzzBov Apr 24 '13 at 05:05
  • @zerkms - I added a note about extending built-in objects. – jfriend00 Apr 24 '13 at 05:05
  • 1
    @zzzzBov - I added a note about changing the argument to `.split()` if the OP wants to split on a different criteria. The OP accepted the answer so presumably it provided at least the core info they wanted. – jfriend00 Apr 24 '13 at 05:12
1

It's not a good idea to override the prototypes of base types unless you're shimming in functionality for older browsers (such as with Array.prototype.indexOf).

It would make sense to simply create a reverse function:

function reverse(str) {
    return str.split(' ').reverse().join(' ');
}

Which could be called as:

reverse('some string literal');

and would produce

'literal some string'

But if you really must have it accessible via dot notation from any string, you could set the function on String.prototype:

String.prototype.reverse = function () {
    return this.split(' ').reverse().join(' ');
};

This particular reverse method is misleadingly similar to Array.prototype.reverse. Adding such a feature could easily be misunderstood by other developers and lead to buggier code.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367