Our framework should provide a compact notation for declaring a class and it should automate all of the operations necessary to create the class. Here is an example of a class declaration.
xclass.declare("Shape", XObject, {
constructor: function(origin) {
this.origin = origin;
console.log("constructing a shape at " + this.origin.toString());
},
draw: function() {
console.log("Drawing the shape at: " + this.origin.toString());
}
});
This example declares a class called Shape which extends XObject. It defines two methods. The constructor method will be added to the chain of pseudo-constructors mentioned earlier. The draw method will be merged into Shape's prototype. The next example declares a Rectangle class that extends Shape.
xclass.declare("Rectangle", Shape, {
constructor: function(origin, bottomRight) {
this.bottomRight = bottomRight;
console.log("constructing a rectangle at " + this.origin.toString());
},
draw: function() {
console.log("Drawing the rectangle at: " + this.origin.toString());
}
});
This example overrides the draw method of Shape. Observe that the constructor only stores the value of bottomRight yet it references this.origin. We can assume the Shape constructor executes before the Rectangle constructor. The value of origin was stored in the Shape constructor so it is safe to reference it in the Rectangle constructor.
Next we will examine how the declare method works.