Home

Dan Stocker edited this page Dec 27, 2013 · 29 revisions
Clone this wiki locally

History

Reference

Download

Example

Sneak a taste. Here's how simple it is to implement a class with Troop.

var nameSpace = {};
// defines a postponed 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!"