我想给Number类型添加一个新的方法,然后用它来改变它自己的值。我写了下面的代码:
Number.prototype.maxx = function(N) {
if (N > this.valueOf()) {
//change this number value to N
}
}
var X = 5;
X.maxx(7);
console.log(X); //expect to show 7
我尝试使用下面的代码来更改值
this = N;
或者
this.valueOf(N);
或者
this.value = N; //of course number do not have any property named 'value'. but I just try it!
但它们都不起作用。
您能给我一些提示吗? 谢谢,
最佳答案
Number.prototype.maxx = function(N) {
if (N > this.valueOf()) {
return N;
} else {
return this.valueOf();
}
}
var X = 5;
X = X.maxx(4);
我想在这里强调的一件事是,当您调用 X.maxx 时,您无法更改此值。相反,您必须将从方法返回的值重新分配给 X。
关于javascript - Node.js 更改原型(prototype)内的 Number 对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44736780/