Updating object value from prototype function

后端 未结 4 359
余生分开走
余生分开走 2020-12-20 01:32

I\'m trying to do this:

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

var temp = \'Hell         


        
4条回答
  •  长情又很酷
    2020-12-20 02:15

    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);
    

提交回复
热议问题