Created
March 29, 2011 14:44
-
-
Save juandopazo/892470 to your computer and use it in GitHub Desktop.
Classes in JavaScript draft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* GOALS: | |
* | |
* - Properties/methods should be either public or private by default. They've been public since the | |
* begining of JS, so maybe they should stay that way and a "private" keyword should be added. | |
* Otherwise, they could be private by default and have a "public" keyword | |
* - Avoid the "var" keyword in object initializer extensions | |
* - Provide a shorter version to be used by compilers or to be mixed by developers | |
* | |
*/ | |
class Point { | |
static method fromString(s) { | |
return new Point(parse(s)); | |
}, | |
/* | |
* Why "static" and not "class" | |
* IMO class introduces a readability problem when used to describe a property. For example: | |
* | |
* class method render () {} // reads fine | |
* | |
* class var render: function () {} // reads fine, but the goal is to avoid the "var" keyword | |
* | |
* class render: function () {} // reads too much like a class definition | |
*/ | |
private method repaint() { // in-place private definitions work wonders | |
/* repaint */ | |
}, | |
new (x, y) { | |
/* | |
* The "this" keyword can't be avoided. It expreses very well that we're making changes at the instance level | |
*/ | |
this.move(x, y); | |
}, | |
/* | |
* This is a prototype method that is configurable, enumerable and writable | |
* Essentially method move(){} is equal to | |
* sealed move: function () {} | |
*/ | |
move: function (x, y) { | |
this.x = x || Point.x; | |
this.y = y || Point.y; | |
this.repaint(); | |
}, | |
static x: 10, | |
static const Y: 20 | |
} | |
/** | |
* As mentioned before, mixins could be great additions | |
* The "uses" keyword has been used by documentation generators to describe mixins for a long time (JSDoc, YUI Doc) | |
*/ | |
frozen class Point3D extends Point uses Comparable, Measurable { //Pretty sure this comma introduces parser problems, | |
//but I can't say for sure | |
static instances: [], | |
new (x, y, z) { | |
this.z = z; | |
super(x, y); | |
/* | |
* I don't think it's necessary to add extra sugar to refer to class (static) properties/methods | |
*/ | |
Point3D.instances.push(this); | |
} | |
} | |
/* | |
* Minified version (as in "javascript as a compilation target") | |
*/ | |
class Point { | |
/** | |
* @ means class/static | |
* | |
* I don't think # should have a different meaning than "function", so in this context it could very easily serve as | |
* a shorthand for "method" | |
*/ | |
@# fromString(s) { | |
return new Point(parse(s)); | |
}, | |
/* | |
* The JavaScript community has been using _ as an informal standard to refer to private properties for a long while | |
* I'm sure _# would be interpreted as a string by a parser, so a new character must be chosen, for example ! | |
*/ | |
!# repaint() {}, | |
new (x, y) { | |
this.move(x, y); | |
}, | |
# move(x, y) { | |
this.x = x || Point.x; | |
this.y = y || Point.Y; | |
this.repaint(); | |
}, | |
@x: 10, | |
@const Y: 20 | |
} | |
// Nothing to minify here | |
frozen class Point3D extends Point uses Comparable { | |
@instances: [], | |
new (x, y, z) { | |
this.z = z; | |
super(x, y); | |
Point3D.instances.push(this); | |
} | |
} | |
// Testing how it works without spaces and comments | |
class P{@#fromString(s){return new P(p(s))},_#r(){},new(x,y){this.x=x||P.x;this.y=y||P.Y;this.r();},@x:10,@const Y:20} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment