Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28790 > unrolled thread

Why JavaScript 'seems' to get addition wrong

Started byAbdulFattah Popoola <abdulapopoola@gmail.com>
First post2015-10-02 08:07 -0700
Last post2015-10-03 15:55 -0300
Articles 5 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Why JavaScript 'seems' to get addition wrong AbdulFattah Popoola <abdulapopoola@gmail.com> - 2015-10-02 08:07 -0700
    Re: Why JavaScript 'seems' to get addition wrong Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-02 16:16 +0100
      Re: Why JavaScript 'seems' to get addition wrong AbdulFattah Popoola <abdulapopoola@gmail.com> - 2015-10-02 19:22 -0700
      Re: Why JavaScript 'seems' to get addition wrong Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-03 05:24 +0200
        Re: Why JavaScript 'seems' to get addition wrong Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-10-03 15:55 -0300

#28790 — Why JavaScript 'seems' to get addition wrong

FromAbdulFattah Popoola <abdulapopoola@gmail.com>
Date2015-10-02 08:07 -0700
SubjectWhy JavaScript 'seems' to get addition wrong
Message-ID<1588ee9e-9182-41fc-bc23-7b7a33739268@googlegroups.com>
Hello everyone,

One of the most popular gotchas of JavaScript is its behaviour when adding operands. For example, the output of  {} + 2 is different from 2 + {}.

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.

Thanks.

[toc] | [next] | [standalone]


#28791

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-10-02 16:16 +0100
Message-ID<87y4flqp1d.fsf@bsb.me.uk>
In reply to#28790
AbdulFattah Popoola <abdulapopoola@gmail.com> 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.

> 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.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#28803

FromAbdulFattah Popoola <abdulapopoola@gmail.com>
Date2015-10-02 19:22 -0700
Message-ID<37fdfbb3-ac72-4850-ab2b-1b1dcb98f792@googlegroups.com>
In reply to#28791
Great suggestion! Thanks!!

On Friday, 2 October 2015 08:16:23 UTC-7, Ben Bacarisse  wrote:
> AbdulFattah Popoola <abdulapopoola@gmail.com> 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.
> 
> > 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.
> 
> -- 
> Ben.

[toc] | [prev] | [next] | [standalone]


#28805

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-10-03 05:24 +0200
Message-ID<1584254.UGq8RPA9Fy@PointedEars.de>
In reply to#28791
Ben Bacarisse wrote:

> AbdulFattah Popoola <abdulapopoola@gmail.com> 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 <https://www.destroyallsoftware.com/talks/wat> 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] 
<https://docs.python.org/3/reference/datamodel.html?highlight=__add__#emulating-callable-objects> pp.
[2a] <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy> p.
[2b] <http://pointedears.de/wsvn/JSX/trunk/array.js> (jsx.array.Array and 
     jsx.array.BigArray)
[3] <http://PointedEars.de/wsvn/JSX/trunk/math.js>
    <http://PointedEars.de/wsvn/JSX/trunk/math/>
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#28808

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2015-10-03 15:55 -0300
Message-ID<mup8b7$i74$1@speranza.aioe.org>
In reply to#28805
Thomas 'PointedEars' Lahn wrote:
> Ben Bacarisse wrote:
>
>> AbdulFattah Popoola <abdulapopoola@gmail.com> 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 <https://www.destroyallsoftware.com/talks/wat> when
> it was still working ;-))

I recall Brendan Eich commenting about this and other weird examples in 
one of his presentations.


>
>>> 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.

AbdulFattah is often posting at the JS Mentors group which seems to be 
dying out. Maybe this is why he has recurred to c.l.js.
<https://groups.google.com/forum/?fromgroups#!forum/jsmentors>

-- 
Joao Rodrigues

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web