operator overloading proposal

Kevin Barabash kevinb at khanacademy.org
Wed May 18 05:27:05 UTC 2016


It seems like this model can be abused in the same way that having separate
methods for assign operators can be abused.  I think having separate
operators and automatically using `[[plusOp]]` if `[[assignPlusOp]]`
doesn't exist is an easier programming model.  People who need the extra
performance can go the extra mile and define both operators.

On Sun, May 15, 2016 at 9:56 AM, Michael Theriot <
michael.lee.theriot at gmail.com> wrote:

> What if you used a target parameter to indicate assignment?
>
> ```js
> operator +(a, b, target = new obj()) {
>   target.val = a.val + b.val;
>   return target;
> }
>
> // or... no arrow functions...
>
> operator +(other, target = new obj()) {
>   target.val = this.val + other.val;
>   return target;
> }
>
> // or... just a boolean
>
> operator +(other, assign) {
>   let target = assign ? this : new obj();
>   target.val = this.val + other.val;
>   return target;
> }
> ```
>
> And throw a TypeError if an assignment does not return the same object.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160517/adbf78cc/attachment.html>


More information about the es-discuss mailing list