Classes: suggestions for improvement

Allen Wirfs-Brock allen at wirfs-brock.com
Sun Jun 12 21:13:50 PDT 2011


On Jun 12, 2011, at 8:46 PM, Axel Rauschmayer wrote:

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


The correspondence is not quite that straight forward.  A prototype P, as used in self, does seem to frequently subsume the role of a class.  A constructor  generally corresponds to the copy method of such a prototype.


> But then instanceof is still weird – the right hand side should be the prototype and not the constructor.


Instanceof seems a artifact of trying to use constructors in the role of classes.  The corresponding operation to instanceof would be childOf: (meaning inherits from) but self doesn't appear to actually have this without getting a mirror (entering the reflection subsystem).  In Smalltalk class inclusion testing, while possible, was generally frowned upon.   Behavior testing  (send isFoo if you want to see if an object has Foo behavior) was the preferred pattern.

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

This appears to be a situation where self would use multiple parents.


Allen


More information about the es-discuss mailing list