Node.js 在原型中更改 Number 对象值 [英] Node.js change Number object value inside prototype
本文介绍了Node.js 在原型中更改 Number 对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想给 Number 类型添加一个新方法,然后用它来改变它自己的值.我写下面的代码:
I want to add a new method to Number type, and then use it to change its own value. I write below code:
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
我尝试使用以下代码来更改值
I try something like below codes to change the value
this = N;
或
this.valueOf(N);
或
this.value = N; //of course number do not have any property named 'value'. but I just try it!
但它们都不起作用.
你能不能给我一些提示来做这件事.谢谢,
Could you please give me some hint to do this. Thanks,
推荐答案
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.
One thing that I would like to highlight over here is when you call X.maxx you cannot change the value of this. Instead, you will have to re-assign the value being returned back from the method to X.
这篇关于Node.js 在原型中更改 Number 对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文