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