expect var p = new Point(); p._ = new Point(1,2) + new Point(3,4) + new Point(5,6); p._ = new Point(1,2) * new Point(3,4) * new Point(5,6); Credit: Tobias Schneider (def.js) Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 2 / 37
to primitive The plus operator (+) Triggering function/method calls via + Implementing StringBuilder Implementing Point WAT Conclusion Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 3 / 37
objects The following values are primitives: Booleans Numbers Strings undefined null All other values are objects. Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 4 / 37
to objects JavaScript operators always work with primitive values. obj1 op obj2 Convert obj1 and obj2 to primitives. Compute the result. Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 5 / 37
values to primitives ToPrimitive(value, PreferredType?) PreferredType is Number: If value is primitive, return it as is. Otherwise, value is object. Call value.valueOf(). If primitive, return. Call value.toString(). If primitive, return. Otherwise, throw a TypeError. PreferredType is String: swap steps 2 and 3. Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 7 / 37
values to numbers Convert primitive: Argument Result undefined NaN null +0 boolean value true is converted to 1, false is converted to +0 number value no conversion necessary string value parse number ("324" becomes 324) Convert object: call ToPrimitive(obj, Number), convert result to number. Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 8 / 37
values to strings Convert primitive: Argument Result undefined "undefined" null "null" boolean value either "true" or "false" number value the number as a string, e.g. "1.765" string value no conversion necessary Convert object: call ToPrimitive(obj, String), convert result to string. Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 9 / 37
operator (+) value1 + value2 Evaluation: Convert value1 and value2 via ToPrimitive(Number). Either one is string: convert both to string, concatenate. Otherwise: convert both to number, add. Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 13 / 37
call x = f(1) + f(2) Define a setter for property x of global object. Object.defineProperty(this, "x", { set: function (x) { console.log("set: "+x); } }); Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 19 / 37
operand of +, *, etc. Point.prototype.valueOf = function () { Point.operands.push(this); return 3; } Lowest natural number x where the following are all different: x + x x − x x · x x / x Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 28 / 37
The following expressions are all equivalent: +{} Number({}) Number({}.toString()) // {}.valueOf() isn't primitive Number("[object Object]") NaN Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 33 / 37
always work with primitive values Fake operator overloading: fun hack, not much more Instead, use method names (see Java): plus, minus, etc. Axel Rauschmayer (rauschma.de) Fake operator overloading 2012-05-31 36 / 37