You can use the Number object if you want it specifically for a number. If you want it for something else and use only the number as an example, you cannot do this because JavaScript does not support operator overloading.
Itβs best to hide the chain by returning this to the plus function so that you can:
var n1 = new MyNumber(1); var n2 = new MyNumber(2); var n3 = new MyNumber(3); var sum = n1.plus(n2).plus(n3);
You also need to make sure MyNumber creates a new MyNumber and returns this if you do not want to change the numbers.
source share