Path: csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Why JavaScript 'seems' to get addition wrong Date: Sat, 03 Oct 2015 05:24:28 +0200 Organization: PointedEars Software (PES) Lines: 51 Message-ID: <1584254.UGq8RPA9Fy@PointedEars.de> References: <1588ee9e-9182-41fc-bc23-7b7a33739268@googlegroups.com> <87y4flqp1d.fsf@bsb.me.uk> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1443842669 1261 eJwFwQcBwEAIBDBLzziGHKDgX0ITiJGNq8EUh6MRhOigOp8zsW/q9rhUL7J4NCvKX90Xr/cHFDoRWw== (3 Oct 2015 03:24:29 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sat, 3 Oct 2015 03:24:29 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwYkBwCAIA8CV0EhIx1Ge/UfonYOLGYfO4+Oj+CpzLDdlfRWRexUcDRp6qpFvw+99LnUNhTKT8Cr54gdgMBX6 Cancel-Lock: sha1:pP+9QsiqTWbPQY1Q33GMQDHhwO0= X-NNTP-Posting-Host: eJwNyccRwEAIBLCWiAsuhzD0X8JZX7mCMWFwmJ8fSZPmSu8yPGSJGje1XClfURarzl8WbCf9ABW5EKw= Xref: csiph.com comp.lang.javascript:28805 Ben Bacarisse wrote: > AbdulFattah Popoola writes: >> One of the most popular gotchas of JavaScript is its behaviour when >> adding operands. For example, the output of {} + 2 is different from 2 >> + {}. > > That's not an example of addition. I think it's better to say that one > of the gochas of EcmaScript is that + means more than one thing. If it were another programming language, you might just say that the operator “+” is overloaded: it does either addition or string concatenation in ECMAScript, depending on the type of its operands: window.alert(Array(16).join("wat" - 1) + " Batman!"); (Shamelessly stolen from when it was still working ;-)) Unfortunately, with few exceptions (the toString() and valueOf() methods of objects), in general one cannot define (this) operator overloading in ECMAScript. (It would be easy if they [ECMA TC39] specified it the Python way, with Specification algorithms supporting __add__(), __sub__(), etc. methods on objects. [1] Some of that is now even specified in ECMAScript 2015, with Proxy [2a], and you can make use of that since quite a while [2b].) But one can implement operators as methods, and call methods instead of using operators. I have done that in the math library of JSX [3]. >> I explained why the puzzling results occur in this post: >> http://abdulapopoola.com/2015/09/30/why-javascript-seems-to-get-addition-wrong/. >> >> Please do share your feedback. > > Share you explanation here, and I'll share my feedback here. They are posting via Google Groups; you can be quite certain that they do not even know what “here” (Usenet) actually is. __________ [1] pp. [2a] p. [2b] (jsx.array.Array and jsx.array.BigArray) [3] -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.