Because Javascript will call the .valueOf method for an object to coerce the value before applying most operators (like basic math ops, binary ops, comparison ops), you can get tricky and change the value returned on the left to the result of a different operation.

```    var obj1 = {
valueOf: function () {
console.log("valueOf1");
return 1;
}
};
var obj2 = {
valueOf: function () {
console.log("valueOf2");
return 2;
}
};

```
And now:
```obj1 + obj2
valueOf1
valueOf2
3
```
Because these are called in order, obj1 can set a flag in the prototype and also store it's value in the prototype, but return nothing. Then obj2 will know it's not first, and can do the actual operation, and return the desired complete value, using obj1's value and it's own.
+

Code:

• ```// This example implements a point class that can do basic math functions,
// but uses both the x and y elements of the point correctly. It uses a fake
// return value to detect the operation between the objects according to the
// resulting calculation, then replaces it in the end with the same sort of
// operation, but done with the actual point values.

function Point(x, y) {
if (arguments.length === 0) {
x = 0;
y = 0;
} else if (arguments.length !== 2) {
throw new Error("Need either 0 or 2 arguments");
}
this.x = x;
this.y = y;
}

Point.operands = [];

Point.prototype.valueOf = function () {
Point.operands.push(this);
// Lowest natural number x where the following are all different:
// x + x, x - x, x * x, x / x
return 3;
}
Object.defineProperty(Point.prototype, "_", {
set: function (value) {
var ops = Point.operands;
var operator;
if (ops.length === 2 && value === 0) { // 3 - 3
operator = this.setSubtract;
} else if (ops.length === 2 && value === 1) { // 3 / 3
operator = this.setDivide;
} else if (ops.length >= 2 && (value === 3 * ops.length)) { // 3 + 3 + 3 + ...
} else if (ops.length >= 2 && (value === Math.pow(3, ops.length))) { // 3 * 3 * 3 * ...
operator = this.setMultiply;
} else {
throw new Error("Unsupported operation (code "+value+")");
}
Point.operands = []; // reset
return operator.apply(this, ops);
},
/**
* "" + mypoint won't invoke toString(), but valueOf().
* Work-around: "" + mypoint._
*/
get: function () {
return this.toString();
}
});

//-------------- Operator implementations

Point.prototype.setSubtract = function (l, r) {
this.x = l.x - r.x;
this.y = l.y - r.y;
return this;
}
Point.prototype.setDivide = function (l, r) {
this.x = l.x / r.x;
this.y = l.y / r.y;
return this;
}
Point.prototype.setAdd = function (first) {
this.x = first.x;
this.y = first.y;
[].slice.call(arguments, 1).forEach(function (op) {
this.x += op.x;
this.y += op.y;
}, this);
return this;
}
Point.prototype.setMultiply = function (first) {
this.x = first.x;
this.y = first.y;
[].slice.call(arguments, 1).forEach(function (op) {
this.x *= op.x;
this.y *= op.y;
}, this);
return this;
}

//-------------- Various helpers

Point.prototype.toString = function () {
return "Point("+this.x+", "+this.y+")";
}
Point.prototype.equals = function (other) {
return this.x === other.x && this.y === other.y;
}

//-------------- Sample use

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

p._ = a + b + c;
console.log(p.toString());

p._ = a * b * c;
console.log(p.toString());

//p._ = a * b + c //can't do this, but...
p._ = a * b
p._ = p + c;
console.log(p.toString());```
+

 file: /Techref/language/java/script/operator-overloading.htm, 4KB, , updated: 2023/5/11 21:41, local time: 2024/2/20 21:33, owner: JMN-EFP-786, TOP NEW HELP FIND:  3.237.34.21:LOG IN

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here:
if you want a response, please enter your email address:
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

### Welcome to sxlist.com!

Site supported by
& kind contributors
just like you!

(here's why

Copies of the site on CD
are available at minimal cost.

.