Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Created October 31, 2011 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jashkenas/1327580 to your computer and use it in GitHub Desktop.
Save jashkenas/1327580 to your computer and use it in GitHub Desktop.
// From: https://mail.mozilla.org/pipermail/es-discuss/2011-October/thread.html#17696
const className = superClass <| function(/*constructor parameters */) {
//constructor body
super.constructor(/*arguments to super constructor */);
this.{
//per instance property definitions
};
}.prototype.{
//instance properties defined on prototype
}.constructor.{
//class (ie, constructor) properties
};
@addyosmani
Copy link

This is truly frightening. Sadly it doesn't look like a huge number of the ideas in the rest of the associated thread (https://mail.mozilla.org/pipermail/es-discuss/2011-October/017758.html) are that much better.. :(

@DavidBruant
Copy link

It seems that you both have a lot of knowledge in what isn't good as a class syntax. I would guess you have as much knowledge on what is good. Could you please enlighten the es-discuss thread? It is open to ideas from anyone.

@jashkenas
Copy link
Author

@DavidBruant: I'm wary of wading in to es-discuss when it's easier to snipe from the sidelines ;)

But in all seriousness, my ideas about a rough direction for JavaScript classes that desugar into pure ES3 prototypes are pretty well covered here: http://jashkenas.github.com/coffee-script/#classes (If you add back JS function syntax, and some braces around the class body).

@jblanche
Copy link

It sure looks weird to me !
Why not stick with http://wiki.ecmascript.org/doku.php?id=harmony:classes#the_proposal_in_a_nutshell
That looks way better, doesn't it ?

@addyosmani
Copy link

I've been playing around with the (current) Harmony class implementation proposal up on http://wiki.ecmascript.org/doku.php?id=harmony:classes#the_proposal_in_a_nutshell and I personally found it very usable and easy to grasp. I wouldn't change that (the superclass stuff earlier in this thread however I am not a fan of).

@jashkenas looking through the other libraries referenced on http://jashkenas.github.com/coffee-script/#classes, is there any one you would say closely matches the implementation of syntactic class sugar you offer through CS at the moment?. I ask as it would be interesting to know from a JS-perspective for anyone that does want to use 'classes' without explicitly coding with prototypal inheritance in mind (even if that does happen behind the scenes).

@jashkenas
Copy link
Author

@addyosmani: All of the various class libraries work more or less the same way, with different levels of extra features. Resig's simple inheritance is about as minimal as it gets, whereas JS.Class has all kind of extra hooks and goodies in it.

CoffeeScript's classes are just simple proper prototype chain inheritance, combined with sugar for easily calling super. The main bonus feature that isn't (and can't be) mimicked in a JavaScript library is executable class bodies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment