Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to define custom operators between instances of a type in JavaScript?

For example, given that I have a custom vector class, is it possible to use

vect1 == vect2

to check for equality, whilst the underlying code would be something like this?

operator ==(a, b) {
    return a.x == b.x && a.y == b.y && a.z == b.z;
}

(This is nonsense of course.)

Thanks.

share|improve this question

4 Answers

up vote 5 down vote accepted

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.

share|improve this answer
1  
I recently discovered chaining by using JQuery. It is extremely useful in certain cases. Thanks a lot for the sample! – pimvdb Jan 21 '11 at 10:46

No, JavaScript doesn’t support operator overloading. You will need to write a method that does this:

Vector.prototype.equalTo = function(other) {
    if (!(other instanceof Vector)) return false;
    return a.x == b.x && a.y == b.y && a.z == b.z;
}

Then you can use that method like:

vect1.equalTo(vect2)
share|improve this answer
Yes, I currently have a function in the prototype as well. However, I'm used to using == for equality checking. But if it is not possible, then I'll just forget about it – pimvdb Jan 15 '11 at 14:31

The best you can do if you want to stick with the == operator:

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

Vector.prototype.toString = function () {
  return this.x + ";" + this.y + ";" + this.z;
};

var a = new Vector(1, 2, 3);
var b = new Vector(1, 2, 3);
var c = new Vector(4, 5, 6);


alert( String(a) == b ); // true
alert( String(a) == c ); // false
alert( a == b + "" );    // true again (no object wrapper but a bit more ugly)
share|improve this answer
That's also a possiblity, but a little bit ugly to be honest. I think I'll be better of using an equality function. – pimvdb Jan 15 '11 at 15:20
It's not that ugly as you might think, because you may already have a toString function on your object for easier debugging. But then again, I wrote this answer just because you said you prefer the == operator to eq. functions. Use what you like the best. Cheers! :) – galambalazs Jan 15 '11 at 15:25
I do have a toString function indeed, but for equality checking it is easier to do v1.equalsTo(v2) than remembering you have to convert it to a String, as that is not the usual way of doing it - thanks alot anyways! – pimvdb Jan 15 '11 at 15:27
+1 for a clever solution (even though it's definitely not one I'd ever want to use. – ForbesLindesay Dec 19 '11 at 15:48

No, it's not part of the spec (which doesn't mean that there aren't some hacks).

share|improve this answer
That's nicely found, but the == operator does not seem to be 'hackable'. – pimvdb Jan 15 '11 at 14:33
3  
i really wish this link still worked – sfjedi Dec 21 '11 at 0:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.