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


Groups > comp.lang.javascript > #25983

Re: ECMAScript and classes

From "Christoph M. Becker" <cmbecker69@arcor.de>
Newsgroups comp.lang.javascript
Subject Re: ECMAScript and classes
Date 2014-10-24 19:45 +0200
Organization solani.org
Message-ID <m2e36p$sgv$1@solani.org> (permalink)
References <dtac4adh7n65csjtrqt7lg59tbkfdv01kd@4ax.com> <m25fv6$kmt$1@solani.org> <f82i4ath9p68jrsomcgg5atkdgrrb9v2rn@4ax.com> <m2basb$ru4$1@solani.org> <4fak4ap1hljij9ea0ae6i2bamg02n6fk0u@4ax.com>

Show all headers | View raw


John Harris wrote:

> On Thu, 23 Oct 2014 18:37:40 +0200, "Christoph M. Becker"
> <cmbecker69@arcor.de> wrote:
> 
>> Usually you wouldn't seal the class instances, though, but rather
>> prevent their extension and maybe remove the configurable attribute on
>> the individual properties.
> 
> But that's what seal does!

Of course, you're right.

>> The "Poor Old Programmer" could, however, write a function that does all
>> this for him.
> 
> And then remember to write the code in the constructor to call the
> function.

No, I meant something like

  function defineClass(...) {...}

There are numerous variants available.

> Multiple constructors, on the other hand, give you a choice of
> parameters. You can already have  new Thing()  and  new Thing(3,4)  by
> putting code in the constructor to look at the argument list and then
> replacing missing arguments by default values : oh look, multiple
> constructors.

Well, as ECMAScript function signatures are quite flexible, you don't
necessarily need multiple constructors, and you won't have any improved
type safety by having them.

> Occasionally, though, you would like the same number of parameters of
> the same type with different meanings. E.g A new complex number given
> the (x,y) values or the (modulus,angle) values. 

You could create functions as methods of the constructor ("static
methods"), each of them converting the arguments as needed and calling
the constructor with the new operator, for instance:

  Complex.createCartesian = function (x, y) {...}
  Complex.createPolar = function (modulus, angle) {...}

> Full support would include creating the new object's properties,
> presumably holding a default value such as undefined. This would also
> mean that the property names are listed in the class description, as
> they should be.

I agree that this would be nicer, but you could use a class defining
function to have a similar notation.

> And wouldn't it be nice if you could write x instead of this.x inside
> methods.

AFAIK, the need to qualify instance properties is quite common in
"dynamic" languages.  And at least it has also some advantages.

Note, that I do not argue that support for classes in ECMAScript
shouldn't be improved, but rather that one has to take what he gets, and
that there are viable ways to deal with the limitations (at least in ES 5).

-- 
Christoph M. Becker

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


Thread

ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-10-21 10:49 +0100
  Re: ECMAScript and classes Gregor Kofler <usenet@gregorkofler.com> - 2014-10-21 12:25 +0200
    Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-10-23 11:34 +0100
  Re: ECMAScript and classes "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-10-21 13:27 +0200
    Re: ECMAScript and classes Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-21 19:27 +0200
      Re: ECMAScript and classes Stefan Weiss <krewecherl@gmail.com> - 2014-10-22 00:15 +0200
      Re: ECMAScript and classes "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-10-23 14:45 +0200
      Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-10-23 15:12 +0100
        Re: ECMAScript and classes Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-01 20:59 +0100
          Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-11-03 15:56 +0000
            Re: ECMAScript and classes Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-08 19:11 +0100
              Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-11-10 14:48 +0000
                Re: ECMAScript and classes Tim Streater <timstreater@greenbee.net> - 2014-11-10 15:54 +0000
                Re: ECMAScript and classes "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-10 18:02 +0100
                Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-11-11 11:08 +0000
                Re: ECMAScript and classes "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 17:48 +0100
                Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-11-13 20:32 +0000
                Re: ECMAScript and classes Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-10 23:03 +0100
                Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-11-11 17:03 +0000
    Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-10-23 14:58 +0100
      Re: ECMAScript and classes "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-10-23 18:37 +0200
        Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-10-24 11:30 +0100
          Re: ECMAScript and classes "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-10-24 19:45 +0200
            Re: ECMAScript and classes John Harris <niam@jghnorth.org.uk.invalid> - 2014-10-26 10:43 +0000

csiph-web