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


Groups > comp.lang.javascript > #30447

Re: Newbie trying to understand object oriented programming

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Newbie trying to understand object oriented programming
Date 2016-05-16 21:39 +0200
Organization PointedEars Software (PES)
Message-ID <1556545.vah8PNoZeM@PointedEars.de> (permalink)
References (7 earlier) <76b604b4-565c-4125-ab78-5b1ef68b5b56@googlegroups.com> <3afrbbhcujk6pjslkg1h5nj2gj4768eqpc@4ax.com> <e6fae746-a30c-4c23-ab93-e3a2b3e8b589@googlegroups.com> <hufubblbp6h65meriph2c6o1u0a88n23co@4ax.com> <d2ed95f3-8320-42cb-baf2-50a9f7e80ee2@googlegroups.com>

Show all headers | View raw


Michael Haufe (TNO) wrote:

> On Saturday, February 13, 2016 at 8:40:30 AM UTC-6, John Harris wrote:
>> I'm pointing out that C++ will let you write
>>   Complex a,b,c;     ...  a = b + c;
>>   Polynomial d,e,f;  ...  d = e + f;
>>   Quaternion g,h,i;  ...  g = h + i;
>> not
>>   a = addComplex(b, c);
>>   d = addPoly(d, f);
>>   g = addQuaternion(h, i);
>> It's easier to write, and easier to proof-read, which is what matters.
> 
> If it's simply a preference of notation, fine; but semantically
> equivalent. […]

JFTR, after loading JSX:math/complex.js [1], you can do

  var
    /* Import into local namespace, shortens the effective scope chain */
    Complex = jsx.math.complex.Complex,

    /* “new” is optional; the constructor can be called as a function */
    a = Complex(1, 2),

    /* Do you think that “Complex("3 + 4j")” should work, too? */
    b = Complex.parse("3 + 4j"),

    c = a.add(b);

  /* jsx_math_complex_Complex {re: 4, im: 6}, "4+6j" */
  console.log(c, c.toString());

which is how this should be done in OOP instead of several add*() functions 
with different names.

The same could be done with polynomials and quarternions.  So it is just 
calling an add() method on an object instead of using the “+” operator; 
(subtype) polymorphism instead of operator overloading (which in C++ really 
is nothing else than ad-hoc polymorphism: you have to declare an operator as 
a function that takes arguments of specific types).

<https://en.wikipedia.org/wiki/Polymorphism_(computer_science)>

[As a side effect of Complex.prototype.valueOf() currently calling 
Complex.prototype.abs(), “a + b” yields the sum, and “a - b” the difference, 
of the absolutes of the two complex numbers, for example allowing them to be 
sorted ascending by their distance from the origin with

  […, …].sort(function (a, b) { return a - b; })

If the imaginary parts are zero, the aforementioned expressions yield the 
correct real sum and differences of the two complex numbers without 
explicitly calling a method.]

It would be nice if ECMAScript introduced general operator overloading (not 
just for unary “+”, and “-”, and concatenating “+”) with __*() methods as 
does Python, but we are not there yet.  Maybe in ECMAScript 2017 ;-)

_________
[1] <http://PointedEars.de/wsvn/JSX/trunk/math/complex.js>
    (the more up-to-date Git repository is not publicly available yet,
    but this is currently the latest revision of this module)
-- 
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.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie trying to understand object oriented programming bit-naughty@hotmail.com - 2016-02-05 06:55 -0800
  Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-05 18:56 +0000
  Re: Newbie trying to understand object oriented programming Tim Slattery <tim@risingdove.com> - 2016-02-05 14:07 -0500
  Re: Newbie trying to understand object oriented programming Aleksandro <aleksandro@gmx.com> - 2016-02-05 17:34 -0300
    Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-05 19:10 -0800
      Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-06 08:33 +0000
        Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-06 08:24 -0800
          Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-06 18:48 +0000
            Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-06 12:28 -0800
              Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-06 21:01 +0000
                Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-07 15:23 -0800
                Re: Newbie trying to understand object oriented programming Aleksandro <aleksandro@gmx.com> - 2016-02-08 11:45 -0300
            Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-08 13:39 -0800
              Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-11 08:09 -0800
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-11 12:22 -0800
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-12 11:09 +0000
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-12 13:34 -0800
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-13 14:40 +0000
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-14 10:18 -0800
                Re: Newbie trying to understand object oriented programming Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-14 21:27 +0100
                Re: Newbie trying to understand object oriented programming "J. Clarke" <j.clarke.873638@gmail.com> - 2016-05-14 10:13 -0400
                Re: Newbie trying to understand object oriented programming Tim Streater <timstreater@greenbee.net> - 2016-05-14 16:47 +0100
                Re: Newbie trying to understand object oriented programming "J. Clarke" <j.clarke.873638@gmail.com> - 2016-05-15 07:05 -0400
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-14 10:08 -0700
                Re: Newbie trying to understand object oriented programming "J. Clarke" <j.clarke.873638@gmail.com> - 2016-05-15 07:02 -0400
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-15 13:11 -0700
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-05-15 17:03 +0100
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-15 13:20 -0700
                Re: Newbie trying to understand object oriented programming Gene Wirchenko <genew@telus.net> - 2016-05-16 11:55 -0700
                Re: Newbie trying to understand object oriented programming Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-05-16 21:39 +0200
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-17 07:30 -0700
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-05-17 15:34 +0100
                Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-13 17:50 -0800
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-14 11:15 +0000
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-14 11:03 -0800
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-15 20:18 +0000
                Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-13 16:54 -0800
  Re: Newbie trying to understand object oriented programming Danny <dann90038@gmail.com> - 2016-02-05 15:00 -0800
  Re: Newbie trying to understand object oriented programming RobG <rgqld@iinet.net.au> - 2016-02-06 17:18 +1000
    Re: Newbie trying to understand object oriented programming Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-06 16:01 +0100
    Re: Newbie trying to understand object oriented programming bit-naughty@hotmail.com - 2016-02-07 04:47 -0800
      Re: Newbie trying to understand object oriented programming Tim Streater <timstreater@greenbee.net> - 2016-02-07 13:16 +0000
      Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-07 17:29 +0000
        Re: Newbie trying to understand object oriented programming bit-naughty@hotmail.com - 2016-02-08 08:53 -0800
          Re: Newbie trying to understand object oriented programming Tim Streater <timstreater@greenbee.net> - 2016-02-08 17:33 +0000
  Re: Newbie trying to understand object oriented programming Joao Rodrigues <jr@none.com> - 2016-02-06 11:56 -0200

csiph-web