1

I am still getting used to Javascript (I'm coming from C++) and would like to add a new function to String which mutates the string instance directly. Let's say that I want to add a new character at the midpoint of the string (ignoring any error checking). In C++ you could do something like this->value = .... Is that the way to do this in Javascript? TIA

String.prototype.mutateSelf = function(param1) {
  // How do I mutate this specific string instance?
  return this;
};
RobertJoseph
  • 7,968
  • 12
  • 68
  • 113
  • 2
    see : http://stackoverflow.com/questions/51185/are-javascript-strings-immutable-do-i-need-a-string-builder-in-javascript – Hacketo Feb 19 '16 at 13:39
  • You'll almost certainly be returning a *new* string, rather than mutating the instance of the existing string. – Jamiec Feb 19 '16 at 13:39

1 Answers1

4

Javascript strings are immutable. You must construct a new string and return it.

Tomalak
  • 332,285
  • 67
  • 532
  • 628