Classes: suggestions for improvement

Axel Rauschmayer axel at rauschma.de
Sun Jun 12 20:46:37 PDT 2011


> So, there is pretty much a directly correspondence between a self copy method and a JavaScript constructor function.  However, there is definitely a difference of emphasis seen here.  In self, it is the prototype object that is the focus of attention.  In traditional JavaScript it is the constructor object.

So you argue that a constructor C corresponds to an instance, a prototype P corresponds to a class (for lack of a better term). And instead of copying the prototypical instance, you “send the message ‘new’” to C. But then instanceof is still weird – the right hand side should be the prototype and not the constructor.

I’ve only got one use case for this, but “class methods” would also work better if they could be attached to the prototype (and subject to inheritance) instead of the constructor.

Take a root class Class that knows how to extend classes, including itself. For example:

var MySuperClass = Class.extend(...);
var MySubClass = MySuperClass.extend(...);

To implement something like this in ES5, you have to manually add the extend() method to each newly created class, because a constructor does not inherit methods that are attached to its super-constructor.

Well, this might be a largely academic discussion and class literals are probably a good enough compromise. But class literals being so similar to object literals, I like the idea of making do with just concise object literals and a proto operator.

Axel

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com





More information about the es-discuss mailing list