Namespace Class
Global namespace for class related functions.
Defined in: jstraits.js.
Constructor Attributes | Constructor Name and Description |
---|---|
Global namespace for class related functions.
|
Method Attributes | Method Name and Description |
---|---|
<static> |
Class.define(o)
Define a new class.
|
Method Detail
<static>
{Function}
Class.define(o)
Define a new class. In the traits model of object oriented programming
a class consists of:
- a superclass
- a set of composed traits
- a collection of methods and state variables
A number of special properties are assigned to the class at definition time:
- A static reference 'superclass' is added which points at the superclass's constructor function.
- The static 'constructor' reference is pointed at the class itself so that the 'typeof' and 'instanceof' operators will behave as expected.
- A static and instance method 'does' is added as well. This method behaves just like Trait#does with the notable difference that it also includes any superclasses's traits in its output.
- Finally, an instance method '_super' is added such that invoking it in any other instance method will call the first function with the same name defined in the class's superclass prototype chain.
Conflicts among instance properties are resolved in the following order: class members override trait members and superclass members, and trait members override superclass members.
The class constructor is specified as a method named init passed into the o.members argument.
var HappyFace = Class.define({ superclass: Doodle, uses: [ TFace, TColoredCircle.aliases({drawColoredCircle: 'draw'}) ], members: { // constructor init: function(color) { this.isDrawn = false; if (color) this.setColor(color); }, // draw a happy face draw: function() { // call Doodle's draw method to set up the canvas this._super(); // draw a colored circle this.drawColoredCircle(); // draw the face this.drawEyes(); this.drawMouth(): // record that the happy face has been drawn this.isDrawn = true; }, // color of the happy face (default is yellow) color: 'yellow', getColor: function() { return this.color }, setColor: function(color) { this.color = color } } }); // draw a blue happy face var hf = new HappyFace('blue'); hf.draw(); log(hf.isDrawn); // => true log(hf.does(TFace)); // => true log(HappyFace.does(TColoredCircle)); // => true log(HappyFace.superclass === Doodle); // => true
- Parameters:
- {Object} o
- The class configuration object.
- {Function} o.superclass Optional
- Superclass from which this class inherits. Default superclass is the global object Class.
- {Trait|Trait[]} o.uses Optional
- A list of traits that will be composed into this class. This happens by first constructing a new anonymous trait and then adding each of that anonymous trait's exported methods into the class prototype. Trait methods are not copied, however, if there is a method defined at the class level with the same name (because class level methods override trait level methods). Unlike normal trait definition, all trait requirements must be fullfilled at class definition time, either by one of the composed traits, a class method, or a superclass method. See the documentation for o.uses in Trait for full details on how to specify this argument.
- {Object} o.members Optional
- Public instance members to be copied into this class's prototype.
- Throws:
- {Trait.TraitError}
- Throws an error if the trait arguments are invalid, there is an unresolved conflict, or there is an unfullfilled trait requirement.
- Returns:
- {Function} Constructor for the newly created class.