Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading Arithmetic Operators in JavaScript?

This is the best way I can think of phrasing this question, given this JavaScript "class" definition:

var Quota = function(hours, minutes, seconds){     if (arguments.length === 3) {         this.hours = hours;         this.minutes = minutes;         this.seconds = seconds;          this.totalMilliseconds = Math.floor((hours * 3600000)) + Math.floor((minutes * 60000)) + Math.floor((seconds * 1000));     }     else if (arguments.length === 1) {         this.totalMilliseconds = hours;          this.hours = Math.floor(this.totalMilliseconds / 3600000);         this.minutes = Math.floor((this.totalMilliseconds % 3600000) / 60000);         this.seconds = Math.floor(((this.totalMilliseconds % 3600000) % 60000) / 1000);     }      this.padL = function(val){         return (val.toString().length === 1) ? "0" + val : val;     };      this.toString = function(){         return this.padL(this.hours) + ":" + this.padL(this.minutes) + ":" + this.padL(this.seconds);     };      this.valueOf = function(){         return this.totalMilliseconds;     }; }; 

and the following test setup code:

var q1 = new Quota(23, 58, 50); var q2 = new Quota(0, 1, 0); var q3 = new Quota(0, 0, 10);  console.log("Quota 01 is " + q1.toString());    // Prints "Quota 01 is 23:58:50" console.log("Quota 02 is " + q2.toString());    // Prints "Quota 02 is 00:01:00" console.log("Quota 03 is " + q3.toString());    // Prints "Quota 03 is 00:00:10" 

Is there any way of implicitly creating q4 as a Quota object using the addition operator as follows...

var q4 = q1 + q2 + q3; console.log("Quota 04 is " + q4.toString());    // Prints "Quota 04 is 86400000" 

rather than resorting to...

var q4 = new Quota(q1 + q2 + q3); console.log("Quota 04 is " + q4.toString());    // Prints "Quota 04 is 24:00:00" 

If not what are the best practice recommendations in this area for making custom numeric JavaScript objects composable via the arithmetic operators?

like image 230
Peter McG Avatar asked Oct 27 '09 23:10

Peter McG


People also ask

What is operator overloading in JavaScript?

ExtendScript allows you to extend or override the behavior of a math or a Boolean operator for a specific class by defining a method in that class with same name as the operator. For example, this code defines the addition (+) operator for the class MyClass .

Is operator overloading supported in JavaScript?

Unlike the other programming languages, JavaScript Does not support Function Overloading.

What is arithmetic overloading?

Overloading Arithmetic Operators as Member Functions This means that a member function to overload a binary arithmetic operator will take one argument rather than the usual two. This also means that the left operand must be an object of our new class in order to overload the operator as a member function of our class.


2 Answers

As far as I'm aware, Javascript (at least as it exists now) doesn't support operator overloading.

The best I can suggest is a class method for making new quota objects from several others. Here's a quick example of what I mean:

// define an example "class" var NumClass = function(value){     this.value = value; } NumClass.prototype.toInteger = function(){     return this.value; }  // Add a static method that creates a new object from several others NumClass.createFromObjects = function(){     var newValue = 0;     for (var i=0; i<arguments.length; i++){         newValue += arguments[i].toInteger();     }     return new this(newValue) } 

and use it like:

var n1 = new NumClass(1); var n2 = new NumClass(2); var n3 = new NumClass(3);  var combined = NumClass.createFromObjects(n1, n2, n3); 
like image 50
Dan Avatar answered Oct 15 '22 14:10

Dan


Unfortunately no.

For fallbacks, if you arranged the return values, you could use method chaining

var q4 = q1.plus(p2).plus(q3); 
like image 42
Justin Love Avatar answered Oct 15 '22 13:10

Justin Love