2

I'm trying to do this:

String.prototype.clear = function(){
    alert(this.value);
    this = ''; // I want to set value to '' here
}

var temp = 'Hello';
temp.clear();// After this step temp should be ''

But I'm getting invalid left hand assignment error. and I found this question as reference, but it's not really what I want. I also find out that 'this' is Immutable.

So, is there any way to do my task? I'm not using it any where. Just playing around. Thanks.

Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57

4 Answers4

1

You can create a class called MyMutableString like:

function MyMutableString(s) {
   this.string = s;
   this.clear = function() {
      this.string = "";
   }
}

Now you create an instance and use it like like:

var s = new MyMutableString("my str");
s.clear(); // makes string stored inside object `s` empty
console.log(s.string);
closure
  • 7,412
  • 1
  • 23
  • 23
1

To make it short:

You can't.

Strings are immutable. Once it is made, a string can never be changed.

You have to set your string to an empty string to "clear" it

var str = "String";
str='';

Note that this won't change the string but sets str to a new Instance of a String, the old one gets garbage collected

It is even shorter then calling a Prototypes method of the Object which would do exactly the same. And you don't have to temper with the native Objects prototypes


Edit - Thx for pointing that out Florian Margaine

If you have many occurences of setting empty Strings and might change your future "definition" of clearing, you could either use a funciton that returns `` or a empty String

function clearString () {
    return ''
}
var str = "Asd"
var str = clearString()

Or simply Set a variable to an empty String

var emptyString = ''
var str = "Asd"
str = emptyString //''

Now if you want to change '' into null or undefined

var emptyString = null

Which would work as well, since primitive data types are being passed as value and not by reference

But i would suggest not modifying an Objects prototype for something like this

Community
  • 1
  • 1
Moritz Roessler
  • 8,542
  • 26
  • 51
0

what about a object copy like this?

String.prototype.clear = function(){
    alert(this);
    return '';
}


var temp = 'Hello';
var newTemp = temp.clear();
alert(newTemp);
silly
  • 7,789
  • 2
  • 24
  • 37
0

Aside from creating a wrapper, the best way is to just go with the functional way.

String.prototype.clear = function() {
    return '';
};

var t = 'hello';
t = t.clear();

Why not just doing t = '';, you say? Well, clearing today might mean putting '', but it might mean putting null tommorrow. Having a single point to change it might be worth it. Or not at all.

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
  • Why do you need to add a prototype method that only rteturns '' you could just set `t = ''` – Moritz Roessler Dec 20 '12 at 13:54
  • @Glutamat yeah, I'd do that. But a method can be convenient. Plus, clearing today might mean putting `''`, but it might have to mean putting `null` tommorrow. – Florian Margaine Dec 20 '12 at 13:55
  • Thats right, nice point, you wouldn't have to change every occurence of `t = ''` to `t = null`, but you could use a simple function or even a variable to return `''` theres no to add methods to `String.prototype` since you can't clear it whit only a method call like `t.clear()` but either way have to redefine it `t = t.clear` =) – Moritz Roessler Dec 20 '12 at 14:14