[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

ECMAScript and classes

John Harris

10/21/2014 9:50:00 AM


ECMAScript and classes

It is sometimes said that "ECMAScript does not have classes". This
is misleading.

ECMAScript is a Turing-complete language that can implement
anything that any other Turing-complete language can implement,
though it could be slower and uglier.

C is another language that "does not have classes", yet the early
C++ compilers translated C++ programs into C programs that were
then compiled by a C compiler into programs that *implemented*
classes.

What ECMAScript and C don't have is full *support* for classes. In
C++, Java, and C# the programmer tells the compiler what each object
in the class should look like, then the compiler writes all the
tedious code that makes it work. This code includes ways for each
object in a class to access the functions (methods) shared by every
member of the class.

As a useful byproduct it is difficult to alter an object's data and
functions in a way that corrupts the intended working of the object.

In contrast, ECMAScript and C have some *support* for classes but
most of the tedious code needed to *implement* a class has to be
written by the Poor Old Programmer.

C has the dot operator and data and function pointers so that shared
functions can be held in one place.

ECMAScript has those and also the 'new' operator. 'new' creates a
new object and then connects it to the functions to be shared with
other objects. But all the object's data fields have to be coded by
the programmer.

As an inconvenient byproduct it is all too easy to alter an object's
data and functions in a way that corrupts the intended working of
the object.

Alternatively, if an object's internal structure is to be changed
after creation then it is not very useful to say it belongs to a
'class'.

To sum up, C++, Java, and suchlike fully *support* classes whereas
ECMAScript allows programmers to *implement* classes but provides
less than full *support* for them.

John
23 Answers

Gregor Kofler

10/21/2014 10:26:00 AM

0

Am 2014-10-21 um 11:49 schrieb John Harris:
>
> ECMAScript and classes
>
> It is sometimes said that "ECMAScript does not have classes". This
> is misleading.

[snip]

The problem is that *constructor functions* are frequently called
"classes". Which they are clearly not.

Gregor

Christoph M. Becker

10/21/2014 11:28:00 AM

0

John Harris wrote:

> To sum up, C++, Java, and suchlike fully *support* classes whereas
> ECMAScript allows programmers to *implement* classes but provides
> less than full *support* for them.

It seems to be important to distinguish between the Ecma-262 editions.
Your notes seem to take into account only Ecma-262 Edition 3. Edition
5.1 (or maybe 5 already) introduced Object.seal()[1] which can be
applied to the prototype object. Ecma-262 Edition 6 might introduce
"maximally minimal" classes[2] and private name objects[3] which would
provide better support for classes in the classical sense.

[1] <http://www.ecma-international.org/ecma-262/5.1/#sec-15...
[2]
<http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_c...
[3] <http://wiki.ecmascript.org/doku.php?id=harmony:private_name_o...

--
Christoph M. Becker

Thomas 'PointedEars' Lahn

10/21/2014 5:27:00 PM

0

Christoph M. Becker wrote:

> John Harris wrote:
>> To sum up, C++, Java, and suchlike fully *support* classes whereas
>> ECMAScript allows programmers to *implement* classes but provides
>> less than full *support* for them.
>
> It seems to be important to distinguish between the Ecma-262 editions.
> Your notes seem to take into account only Ecma-262 Edition 3. Edition
> 5.1 (or maybe 5 already) introduced Object.seal()[1]

5 did, see ES 5.1 Annex F (which does not mention a change there).

> which can be applied to the prototype object. Ecma-262 Edition 6 might
> introduce "maximally minimal" classes[2] and private name objects[3] which
> would provide better support for classes in the classical sense.

Also, implementations of the abandoned ECMAScript Edition 4 Working Draft do
have native and support user-defined classes. You can see that, for
example, with Microsoft JScript .NET in ASP .NET, and ActionScript 2.0+ in
Macromedia/Adobe Flash. The Netscape implementation of that draft was
versioned â??JavaScript 2.0â? (never released), and there was a reference
implementation codenamed â??Epimetheusâ?.

That Edition 6 might (I daresay, probably will) re-introduce classes in some
way appears to be the result of the Harmony agreement:

<https://mail.mozilla.org/pipermail/es-discuss/2008-August/00683...

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Stefan Weiss

10/21/2014 10:15:00 PM

0

On 2014-10-21 19:27, Thomas 'PointedEars' Lahn wrote:
> That Edition 6 might (I daresay, probably will) re-introduce classes in some
> way appears to be the result of the Harmony agreement:
>
> <https://mail.mozilla.org/pipermail/es-discuss/2008-August/00683...

Very likely; it's been in the draft for ECMAScript 6 for a while now.
An unofficial provisional HTML version of the class specification can be
found here:
<http://people.mozilla.org/~jorendorff/es6-draft.html#sec-class-defin...

Some examples and further details (slightly outdated, but close enough
to get a general idea):
<http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_c...

I'm not actively following the ES6 specification process. Some of the
examples I've seen look like a whole new language. This will take some
getting used to... Interesting times.


- stefan

John Harris

10/23/2014 10:35:00 AM

0

On Tue, 21 Oct 2014 12:25:41 +0200, Gregor Kofler
<usenet@gregorkofler.com> wrote:

>Am 2014-10-21 um 11:49 schrieb John Harris:
>>
>> ECMAScript and classes
>>
>> It is sometimes said that "ECMAScript does not have classes". This
>> is misleading.
>
>[snip]
>
>The problem is that *constructor functions* are frequently called
>"classes". Which they are clearly not.

Very true. There's a lot of confusion and muddled thinking in this
area.

Unfortunately, the proposed update to the standard given in

<http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_c...
has a terminology section with the definition :

"class:
The set of objects consisting of a constructor function and
prototype object that is referenced by the function?s ?prototype?
property."

This is even worse than the standard using "Reference" to mean an
object coupled with an identifier (and a marker in later editions).

John

Christoph M. Becker

10/23/2014 12:46:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> Christoph M. Becker wrote:
>
>> It seems to be important to distinguish between the Ecma-262 editions.
>> Your notes seem to take into account only Ecma-262 Edition 3. Edition
>> 5.1 (or maybe 5 already) introduced Object.seal()[1]
>
> 5 did, see ES 5.1 Annex F (which does not mention a change there).

Thanks. Indeed Object.seal() was already part of Edition 5.[1]

[1]
<http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262%205th%20edition%20December%2020...

--
Christoph M. Becker

John Harris

10/23/2014 1:58:00 PM

0


On Tue, 21 Oct 2014 13:27:38 +0200, "Christoph M. Becker"
<cmbecker69@arcor.de> wrote:

>John Harris wrote:
>
>> To sum up, C++, Java, and suchlike fully *support* classes whereas
>> ECMAScript allows programmers to *implement* classes but provides
>> less than full *support* for them.
>
>It seems to be important to distinguish between the Ecma-262 editions.
>Your notes seem to take into account only Ecma-262 Edition 3.

This is the problem for the web designer. Does he tell the
shareholders their company can sell only to people using the latest
whizzo browser; or does he write duplicate code for old and new
browsers; or does he write using just what's common to old and new?

How much does it cost to get reliable market research telling them
which browsers their intended customers use?


>Edition
>5.1 (or maybe 5 already) introduced Object.seal()[1] which can be
>applied to the prototype object.

It wouldn't be just the prototype object. It would be all the
user-defined objects in the prototype chain and each class instance
object as it is created. All of which has to done by the Poor Old
Programmer, of course. But you do this only if you don't trust the
people who will be using the code, or you don't know who they might
be.


>Ecma-262 Edition 6 might introduce
>"maximally minimal" classes[2] and private name objects[3] which would
>provide better support for classes in the classical sense.
<snip>

The current draft ES6 allows only one constructor function per class.
Occasionally this will be inconvenient. And it requires the Poor Old
Programmer to write the code inserting instance properties into a new
object.

So, ES6 will likely still only provide partial *support* for classes,
while still allowing full *implementation* of classes as before.

John

John Harris

10/23/2014 2:12:00 PM

0

On Tue, 21 Oct 2014 19:27:14 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

<snip>
>That Edition 6 might (I daresay, probably will) re-introduce classes in some
>way appears to be the result of the Harmony agreement:
>
><https://mail.mozilla.org/pipermail/es-discuss/2008-August/00683...

Replace
"re-introduce classes"
by
"re-introduce *support* for classes".
But not full support it appears.

John

Christoph M. Becker

10/23/2014 4:38:00 PM

0

John Harris wrote:

> On Tue, 21 Oct 2014 13:27:38 +0200, "Christoph M. Becker"
> <cmbecker69@arcor.de> wrote:
>
>> Edition
>> 5.1 (or maybe 5 already) introduced Object.seal()[1] which can be
>> applied to the prototype object.
>
> It wouldn't be just the prototype object. It would be all the
> user-defined objects in the prototype chain and each class instance
> object as it is created.

Usually you wouldn't seal the class instances, though, but rather
prevent their extension and maybe remove the configurable attribute on
the individual properties.

> All of which has to done by the Poor Old
> Programmer, of course.

The "Poor Old Programmer" could, however, write a function that does all
this for him.

>> Ecma-262 Edition 6 might introduce
>> "maximally minimal" classes[2] and private name objects[3] which would
>> provide better support for classes in the classical sense.
> <snip>
>
> The current draft ES6 allows only one constructor function per class.

This restriction applies to several object-oriented programming
languages with classical inheritance.

> So, ES6 will likely still only provide partial *support* for classes,
> while still allowing full *implementation* of classes as before.

What would be full *support* for classes. Isn't multiple inheritance an
important concept, for instance, which is only supported by very few
languages? What about multiple dispatch?

--
Christoph M. Becker

John Harris

10/24/2014 10:30:00 AM

0

On Thu, 23 Oct 2014 18:37:40 +0200, "Christoph M. Becker"
<cmbecker69@arcor.de> wrote:

>John Harris wrote:
>
>> On Tue, 21 Oct 2014 13:27:38 +0200, "Christoph M. Becker"
>> <cmbecker69@arcor.de> wrote:
>>
>>> Edition
>>> 5.1 (or maybe 5 already) introduced Object.seal()[1] which can be
>>> applied to the prototype object.
>>
>> It wouldn't be just the prototype object. It would be all the
>> user-defined objects in the prototype chain and each class instance
>> object as it is created.
>
>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!


>> All of which has to done by the Poor Old
>> Programmer, of course.
>
>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.


>>> Ecma-262 Edition 6 might introduce
>>> "maximally minimal" classes[2] and private name objects[3] which would
>>> provide better support for classes in the classical sense.
>> <snip>
>>
>> The current draft ES6 allows only one constructor function per class.
>
>This restriction applies to several object-oriented programming
>languages with classical inheritance.

Multiple inheritance in ECMAScript terms would change the prototype
chain into a prototype tree. Not all that difficult to do in
ECMAScript I suspect but very unlikely!

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.

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.


>> So, ES6 will likely still only provide partial *support* for classes,
>> while still allowing full *implementation* of classes as before.
>
>What would be full *support* for classes. Isn't multiple inheritance an
>important concept, for instance, which is only supported by very few
>languages? What about multiple dispatch?

Full support for classes needn't include inheritance : you can have
classes with or without inheritance and I'm concentrating on classes.

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.

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

John