使用原型的Javascript如何设置“this”的值?一个数字? [英] Javascript using prototype how can I set the value of "this" for a number?

查看:82
本文介绍了使用原型的Javascript如何设置“this”的值?一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以如果我们能够超越你应该吗?问题...有没有人知道如何在原型中设置整数的值?

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

Number.prototype.add = function(num){
  var newVal = this.valueOf() + num;
  this.valueOf(newVal);
  return newVal;
}

var rad = 4001.23;
document.write(rad.add(10) + '<br/>' + rad);

你会注意到rad.add(10)返回变量rad加上10所包含的数字,但我真的想改变它原型添加函数中的rad值(我知道this.valueOf(newVal)没有完成)。

You'll notice rad.add(10) returns the number contained in the variable "rad" plus 10, but I would really like to change the value of rad from within the prototype add function (something I know this.valueOf(newVal) does not accomplish).

可以这样做吗?如果是这样,怎么样?

Can this be done? If so, how?

推荐答案

基本上你不能,数字,布尔和字符串是不可变的

Essentially you cant, Numbers, Booleans and Strings are immutable

这篇关于使用原型的Javascript如何设置“this”的值?一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆