Skip to content Skip to sidebar Skip to footer

Node.js Change Number Object Value Inside Prototype

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()) { //c

Solution 1:

Number.prototype.maxx = function(N) {
  if (N > this.valueOf()) {
    return N;
  } else {
    returnthis.valueOf(); 
  }
}

var X = 5;
X = X.maxx(4);

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.

Solution 2:

You cannot change primitive value of the built-in types, as it's stored in a special property (I believe it might be Symbol). Number is immutable as well.

You can still create a method that will return bigger of the two.

Post a Comment for "Node.js Change Number Object Value Inside Prototype"