-
Notifications
You must be signed in to change notification settings - Fork 3
Home
Dan Stocker edited this page Aug 5, 2013
·
29 revisions
JsDoc-based reference arriving soon.
Sneak a taste. Here's how simple it is to implement a class with Troop.
var nameSpace = {};
// promises a class definition
// actual definition will occur on first access
troop.postpone(nameSpace, 'myClass', function () {
// extends base troop class
return troop.Base.extend()
// with a constant ...
.addConstants({
PREFIX: "message:"
})
// ... and methods
.addMethods({
// initializes instance, mandatory method
init: function (message) {
// saves argument in private instance-level property
// 'this' refers to instance
this.addPrivate({
_message: message
});
},
// custom method, constructs and outputs a message
greet: function () {
// _message is the private property added in .init
alert(this.PREFIX + this._message);
}
});
});
// instantiation, also first access to class
// at this point, class is not ready
var myInstance = nameSpace.myClass.create('hello world!');
// calling custom method on instance
myInstance.greet(); // alerts "message:hello world!"