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.

4
  • 3
    Strings are immutable... string objects too. Commented Dec 20, 2012 at 13:43
  • You should not be messing with native object prototypes. Commented Dec 20, 2012 at 13:45
  • It's easier to do just temp = '' ))) Commented Dec 20, 2012 at 13:51
  • Possible duplicate of Set String via String.prototype function without return Commented Nov 10, 2018 at 11:29

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

what about a object copy like this?

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


var temp = 'Hello';
var newTemp = temp.clear();
alert(newTemp);

Comments

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.

3 Comments

Why do you need to add a prototype method that only rteturns '' you could just set t = ''
@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.
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 =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.