Overloading Arithmetic Operators in JavaScript

Overloading Arithmetic Operators in JavaScript?

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);

Javascript: operator overloading

As you've found, JavaScript doesn't support operator overloading. The closest you can come is to implement toString (which will get called when the instance needs to be coerced to being a string) and valueOf (which will get called to coerce it to a number, for instance when using + for addition, or in many cases when using it for concatenation because + tries to do addition before concatenation), which is pretty limited. Neither lets you create a Vector2 object as a result. Similarly, Proxy (added in ES2015) lets you intercept various object operations (including property access), but again won't let you control the result of += on Vector instances.


For people coming to this question who want a string or number as a result (instead of a Vector2), though, here are examples of valueOf and toString. These examples do not demonstrate operator overloading, just taking advantage of JavaScript's built-in handling converting to primitives:

valueOf

This example doubles the value of an object's val property in response to being coerced to a primitive, for instance via +:

function Thing(val) {
this.val = val;
}
Thing.prototype.valueOf = function() {
// Here I'm just doubling it; you'd actually do your longAdd thing
return this.val * 2;
};

var a = new Thing(1);
var b = new Thing(2);
console.log(a + b); // 6 (1 * 2 + 2 * 2)

Can I define custom operator overloads in Javascript?

I agree that the equal function on the vector prototype is the best solution. Note that you can also build other infix-like operators via chaining.

function Vector(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}

Vector.prototype.add = function (v2) {
var v = new Vector(this.x + v2.x,
this.y + v2.y,
this.z + v2.z);
return v;
}

Vector.prototype.equal = function (v2) {
return this.x == v2.x && this.y == v2.y && this.z == v2.z;
}

You can see online sample here.

Update: Here's a more extensive sample of creating a Factory function that supports chaining.

How to overload operator equality for JavaScript objects

You can't overload ==, but == has an implicit .toString() call, so whatever .toString() returns will allow you to effectively overload == (kinda):

function foo(){}
foo.prototype.toString = function(){ return 42; }

var x = new foo();
x == 42; // true

As for how to do this in Dojo, I don't use Dojo, sorry, but the gist is that you get a reference to whatever object is creates and add thatObject.prototype.toString as in my example.

Using Arithmetic operators in parameters of a Function

The answer is: no, you can't put arithmetic operators as a parameter.

As far as I know, there are no languages that gives you ability to calculate as a parameter.

However, you could do something like this if you wish:

function clubMember(clubName, total, women, men) {
const club = `${clubName} club has ${total} members including ${women} Women and ${men} Men`;
return club;
}
clubMember(club, women+men, women, men);

Implementing JS arithmetic operators in node.js C++ addon

Unfortunately the behavior of the + operator is not exposed for modification.

The ECMA-262 spec defining JavaScript defines the + operator as either adding two numbers, or concatenating two strings. To do this, implementations call .toString() and/or .valueOf() on the object, and then perform concatenation or addition. Nothing about the operator itself is ever exposed to the implementation C++ code. The C++ code can only effect the string/value returned for processing.



Related Topics



Leave a reply



Submit