Javascript using prototype how can I set the value of “this” for a number?

前端 未结 3 1986
执笔经年
执笔经年 2021-01-27 16:09

So if we can get past the \"should you?\" question ... does anyone know how to set the value of an integer in prototype?

3条回答
  •  再見小時候
    2021-01-27 16:29

    Number.prototype.add = function(num){
      var newVal = this.valueOf() + num;
      this.valueOf(newVal);
      return newVal;
    }
    
    var rad = new Number(4001.23);
    document.write(rad.add(10) + '
    ' + rad);

提交回复
热议问题