<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>2ality – JavaScript and more - Latest Comments in Fake operator overloading in JavaScript</title><link>http://2ality.disqus.com/</link><description></description><atom:link href="https://2ality.disqus.com/fake_operator_overloading_in_javascript/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Sat, 28 Jul 2018 08:48:07 -0000</lastBuildDate><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-4010533832</link><description>&lt;p&gt;Lol, I made package that overload bracket operator [] &lt;a href="https://github.com/munrocket/overload-bracket" rel="nofollow noopener" target="_blank" title="https://github.com/munrocket/overload-bracket"&gt;https://github.com/munrocke...&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Pavel Melnikov</dc:creator><pubDate>Sat, 28 Jul 2018 08:48:07 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-3962279484</link><description>&lt;p&gt;That operators overload technique is brilliant! Even 6 years later, with new JS standards around it has a perfect sense. Love it!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Sasha Firsov</dc:creator><pubDate>Tue, 26 Jun 2018 17:17:41 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-3718331654</link><description>&lt;p&gt;Not sure how I feel about this, amazing 'hack' but I'm certainly impressed!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrew Keats</dc:creator><pubDate>Sat, 20 Jan 2018 22:21:31 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-3643277682</link><description>&lt;p&gt;Thanks  Axel for this great article.&lt;br&gt;You made my life easy &amp;lt;3&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Misha Sulikashvili</dc:creator><pubDate>Sat, 02 Dec 2017 11:10:14 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-2762724477</link><description>&lt;p&gt;Thanks, Axel. Your site is quickly becoming my 'go-to' reference for all my Javascript questions. Terrific explanations of how to implement all my favorite language features.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Kevin Zeidler</dc:creator><pubDate>Sat, 02 Jul 2016 21:19:22 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-1998809231</link><description>&lt;p&gt;Axel Raushmayer, is it possible run this on browser enviroment. git repo example works on server env .https://&lt;a href="http://github.com/rauschma/op_overload" rel="nofollow noopener" target="_blank" title="github.com/rauschma/op_overload"&gt;github.com/rauschma/op_over...&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Agradip</dc:creator><pubDate>Fri, 01 May 2015 21:45:11 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-1508010430</link><description>&lt;p&gt;This seems similar to the concept of operator overloading I used in &lt;a href="https://github.com/FilipZawada/Fingers.js/" rel="nofollow noopener" target="_blank" title="https://github.com/FilipZawada/Fingers.js/"&gt;Fingers.js&lt;/a&gt; (initially done for &lt;a href="http://filimanjaro.com/fingers" rel="nofollow noopener" target="_blank" title="http://filimanjaro.com/fingers"&gt;AS3&lt;/a&gt;, later ported to JS). It overloads valueOf for adding .NET style event listeners:&lt;/p&gt;&lt;p&gt;&lt;code&gt;&lt;br&gt;function hi() { console.log("hi") }&lt;br&gt;    function twoality() { console.log("2ality") }&lt;br&gt;    function bye() { console.log("bye") }&lt;br&gt;&lt;br&gt;    on(yourButton).click += hi + twoality;&lt;br&gt;    on(yourButton).click -= hi - bye;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;I've got an &lt;a href="http://filimanjaro.com/2012/operators-overloading-in-as3-javascript-too-%E2%80%93-workaround/" rel="nofollow noopener" target="_blank" title="http://filimanjaro.com/2012/operators-overloading-in-as3-javascript-too-%E2%80%93-workaround/"&gt;article&lt;/a&gt; explaining how it works, maybe helpful for some readers.&lt;/p&gt;&lt;p&gt;Cheers!&lt;br&gt;Filip&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Filip Zawada</dc:creator><pubDate>Mon, 28 Jul 2014 07:42:42 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-1439488597</link><description>&lt;p&gt;Some operators even convert their right operands first:&lt;/p&gt;&lt;p&gt;    &amp;gt; func("LEFT") &amp;gt; func("RIGHT")&lt;br&gt;    func_LEFT&lt;br&gt;    func_RIGHT&lt;br&gt;    valueOf_RIGHT&lt;br&gt;    valueOf_LEFT&lt;/p&gt;&lt;p&gt;But when i run it in recent ff\chrome console look like in first case.Why?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">mor</dc:creator><pubDate>Tue, 17 Jun 2014 12:03:12 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-423611866</link><description>&lt;p&gt;There it is short-circuiting plus associativity. It is very unusual for an operator not to first evaluate all of its operands. || and &amp;amp;&amp;amp; are special here. As you mention, || only evaluates its second operand if its first operand is false.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Axel Rauschmayer</dc:creator><pubDate>Sun, 29 Jan 2012 07:44:42 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-423605302</link><description>&lt;p&gt;Fixed the typo, thanks. Can you format your comment with PRE tags? Then it is easier to read.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Axel Rauschmayer</dc:creator><pubDate>Sun, 29 Jan 2012 07:19:16 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-406793452</link><description>&lt;p&gt;Typo    x + x&lt;br&gt;    y - x &amp;lt;- y should be x (produces 0)&lt;br&gt;    x * x&lt;br&gt;    x / xWhile you make clever use of the different signatures the above produce wouldn't italso be possible to achieve a similar effect by using enumeration values?E.I. Object.defineProperty(Point.prototype, "_", {        set: function (value) {            var ops = Point.operands;            var operator;	    switch (value) {		case &lt;a href="http://valEnum.do" rel="nofollow noopener" target="_blank" title="valEnum.do"&gt;valEnum.do&lt;/a&gt; :		console.log('do -&amp;gt; 1');		operator = this.setSubtract;		break;				case valEnum.another :		console.log('another -&amp;gt; 2');		operator = this.setAdd;		break;				case valEnum.thing :		console.log('thing -&amp;gt; 4');		break;				case (valEnum.another+valEnum.thing)		console.log('another thing -&amp;gt; 6');		operator = this.setMultiply;		break;				default:		throw new Error("Unsupported operation (code "+value+")");	    }            Point.operands = []; // reset            return operator.apply(this, ops);        }    });var valEnum = {	do: 1,	another: 2,	thing: 4}Point.prototype.valueOf = function () {        Point.operands.push(this);        return valEnum.another;}On seconds thought.. no not really.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">dotnetCarpenter</dc:creator><pubDate>Mon, 09 Jan 2012 22:38:24 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-393145704</link><description>&lt;p&gt;It's possible to use JSShaper and little AST trickery allow proper operator overloading. See &lt;a href="http://nixtu.blogspot.com/2011/05/using-jsshaper-to-provide-operator.html" rel="nofollow noopener" target="_blank" title="http://nixtu.blogspot.com/2011/05/using-jsshaper-to-provide-operator.html"&gt;http://nixtu.blogspot.com/2...&lt;/a&gt; . I believe this is close to what Dart uses these days.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Juho Vepsäläinen</dc:creator><pubDate>Fri, 23 Dec 2011 11:59:17 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-391717461</link><description>&lt;p&gt;Nice :)&lt;/p&gt;&lt;p&gt;It is the same underlying effect that don't call the method in this example (it stops on the first truthy value) ?&lt;br&gt;myVar || true || myFunc()&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Romain Guerin</dc:creator><pubDate>Wed, 21 Dec 2011 15:46:04 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-390246321</link><description>&lt;p&gt;Yes, interesting. It is associativity at work:&lt;br&gt;a &amp;lt;&amp;lt; b &amp;lt;&amp;lt; c === (a &amp;lt;&amp;lt; b) &amp;lt;&amp;lt; c&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Axel Rauschmayer</dc:creator><pubDate>Mon, 19 Dec 2011 18:18:28 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-390047532</link><description>&lt;p&gt;Something I found interesting:  When you chain further operators (like func("LEFT") &amp;gt;&amp;gt; func("MIDDLE") &amp;gt;&amp;gt; func("RIGHT");) the additional func calls are not made until the first two have concluded their underlying operator business (bitshifting, modulous, etc.).  So, in the above example you get:&lt;br&gt;func_LEFTfunc_MIDDLEvalueOf_LEFTtoString_LEFTvalueOf_MIDDLEtoString_MIDDLEfunc_RIGHTvalueOf_RIGHTtoString_RIGHT&lt;/p&gt;&lt;p&gt;Maybe that has some value... just gotta figure out what that is :P&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">PaulGrenier</dc:creator><pubDate>Mon, 19 Dec 2011 14:17:39 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-389209773</link><description>&lt;p&gt;See the new section “Detecting the operator”.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Axel Rauschmayer</dc:creator><pubDate>Sun, 18 Dec 2011 09:36:13 -0000</pubDate></item><item><title>Re: Fake operator overloading in JavaScript</title><link>http://www.2ality.com/2011/12/fake-operator-overloading.html#comment-388966321</link><description>&lt;p&gt;I met with similar approach when I was trying to find out how operators work in Paper.js.&lt;br&gt;Is there any way how to find out which operator called the valueOf function?&lt;/p&gt;&lt;p&gt;Imagine simple Point object and attempt to perform basic math operation. E.g. p1 + p2 = p3 and p3 has sum x and y coordinates, with this approach it would be possible to make it work for one operator but not for all of them (+; -; /; *; ...).&lt;/p&gt;&lt;p&gt;I would not like to wrap whole piece of code into some function or compile it as paper.js does but it's the only solution is not it?&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Dan</dc:creator><pubDate>Sat, 17 Dec 2011 19:12:28 -0000</pubDate></item></channel></rss>