Skip to content
Dan Stocker edited this page May 8, 2013 · 29 revisions

Download

Contents

Appetizer

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!"
Clone this wiki locally