es-discuss Digest, Vol 56, Issue 4

Axel Rauschmayer axel at rauschma.de
Sat Oct 1 12:42:49 PDT 2011


> From: Russell Leggett <russell.leggett at gmail.com>
> Subject: Minor extension to Set Literal Prototype operator as minimal classes
> Date: October 1, 2011 19:27:27 GMT+02:00
> To: es-discuss <es-discuss at mozilla.org>
> 
> 
> I'm probably not the first to have thought of this, but it seemed like
> the most minimal class support I could think of, and it sounds like
> that's where things are headed from the lack of agreement. Through the
> additions made by the Set Literal Prototype operator proposal, I think
> you nailed 90% of the pain point for most developers. Nicer method
> syntax, simple extension functionality, and the addition of super
> seals the deal. The only thing missing is how to translate that set of
> functionality to work with "classes" instead of just objects. It seems
> so close, in fact, that the idea of doing a class hierarchy in a
> different way seems almost unintuitive.

Note that in JavaScript terminology, a class is a function (which in turn is an object). So there really are no classes. Just functions that produce instances when used with the "new" operator (if a function is used this way, it is often called a constructor). Then the difficulty is how to assemble it all:

1. The super-constructor SuperClass must be the prototype of the sub-constructor ClassName, if class properties are to be inherited.
2. The super-prototype SuperClass.prototype must be the the prototype of ClassName.prototype.
3. (Instance) methods must be added to ClassName.prototype.
4. Class properties must be properties of ClassName (which is a function!).
5. Instance properties must be set up in the constructor.

If you look at Allen’s class pattern then the proto operator <| takes care of #1 + #2, .prototype.{} does #3, .constructor.{} does #4. You have to make sure that the compound expression ends with something that returns the constructor, or else the assignment at the beginning makes no sense.

If you want to read up on this – a while ago, I’ve written a post that might be helpful: http://www.2ality.com/2011/06/prototypes-as-classes.html

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111001/7deaac37/attachment.html>


More information about the es-discuss mailing list