-
Notifications
You must be signed in to change notification settings - Fork 3
Home
danstocker edited this page Oct 31, 2012
·
29 revisions
Troop is an OOP library with a primary goal to give JavaScript programmers a versatile and easy-to-use tool to create clean, object-oriented code. Troop classes consume less memory, and instantiate faster than those created through the revealing pattern or fat constructors. Troop also provides unit testing with simple means to mock methods, and asynchronous module definition (AMD) with classes as promises.
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.promise(nameSpace, 'myClass', function () {
// extends base troop class
return troop.Base.extend()
// with a constant ...
.addConstant({
PREFIX: "message:"
})
// ... and methods
.addMethod({
// 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!"