Class | Desugars |
/* ------------------------------------------------------
* Class Point definition
* ------------------------------------------------------
*/
class Point {
/**
* Class method (placed on constructor function)
*/
@fromString: function (s) {
return new Point(parse(s));
}
/**
* Instance intializer (aka "constructor")
*/
initialize: function (x, y) {
@move(x, y);
}
/**
* Simple instance method (placed on prototype)
*/
move: function (x, y) {
@x = x;
@y = y;
@repaint();
}
/**
* Class property (placed on constructor function)
*/
@x: 10
} |
/* ------------------------------------------------------
* class Point Desugars into
* ------------------------------------------------------
*/
/**
* Instance intializer (aka "constructor")
*/
function Point(x, y) {
this.move(x, y);
}
/**
* Class method (placed on constructor function)
*/
Object.defineProperty(Point, "fromString", {
value: function PointFromString(s) {
return new Point(parse(s));
},
writable: true
});
/**
* Simple instance method (placed on prototype)
*/
Object.defineProperties(Point.prototype, {
move: {
value: function pointMove(x, y) {
this.x = x;
this.y = y;
return this.repaint();
},
writable: true
},
class: {
value: Point // or even Point.prototype (should decide)
},
toString: {
value: function () {
return "[instance #{identity} of Point class]";
}
}
});
/**
* Class property (placed on constructor function)
*/
Object.defineProperty(Point, "x", {
value: 10,
writable: true
});
/**
* Class method (internal things)
*/
Object.defineProperties(Point, {
toString: {
value: function PointToString() {
return "[class Point]";
}
},
instanceMethods: {
value: function () {
// return collection of methods placed on prototype
}
},
classMethods: {
value: function () {
// returns collection of class methods
}
},
super: {
value: null // or Object ?
}
}); |
/* ------------------------------------------------------
* Class Point3D definition
* ------------------------------------------------------
*/
static class Point3D extends Point {
include Comparable;
/**
* Class static property
*/
@instances: []
/**
* Instance intializer (aka "constructor")
*/
initialize: function (x, y, z) {
super(x, y);
@z = z;
@class.instances.push(@); // save the instance
}
}
|
/* ------------------------------------------------------
* class Point3D Desugars into
* ------------------------------------------------------
*/
/**
* Instance intializer (aka "constructor")
*/
function Point3D(x, y, z) {
Point3D.super.call(this, x, y);
this.z = z;
this.class.instances.push(this);
}
/**
* Class method (placed on constructor function)
*/
Object.defineProperty(Point3D, "instances", {
value: []
});
/**
* Class method (internal things)
*/
Object.defineProperties(Point3D, {
toString: {
value: function Point3DToString() {
return "[class Point3D]";
}
},
instanceMethods: {
value: function () {
// return collection of methods placed on prototype
}
},
classMethods: {
value: function () {
// returns collection of class methods
}
},
super: {
value: Point
}
});
/**
* Inheritance
*/
Object.defineProperty(Point3D, "prototype" {
value: Object.create(Point.prototype)
});
/**
* Prototype fixes
*/
Object.freeze(Object.defineProperties(Point3D.prototype, {
class: {
value: Point3D // or even Point3D.prototype (should decide)
},
toString: {
value: function () {
return "[instance #{identity} of Point3D class]";
}
}
}));
Object.freeze(Point3D); // since it's a static class
|
Examples: | |
/* ------------------------------------------------------
* Examples:
* ------------------------------------------------------
*/
// create new 3D point
let point = new 3DPoint(10, 20, 30);
console.log(point); // "[instance 0x1382 of Point3D class]"
// inherited method
point.move(100, 200);
// introspect the class
console.log(point.class); // "[class Point3D]"
console.log(point.class === Point3D); // true
// instrospect the super class
console.log(point.class.super); // "[class Point]"
console.log(point.class.super === Point); // true
console.log(Point3D.super === Point); // true
console.log(point instanceof Point3D); // true
console.log(point instanceof Point); // true
// class method
let p = Point.fromString("10-20");
console.log(p.class); // "[class Point]"
// analyze class methods and properties
console.log(Point.classMethods); // ["fromString"]
console.log(Point.classProperties); // ["x"]
console.log(Point.instanceMethods); // ["move"]
console.log(Point3D.classProperties); // ["instances"]
|