2012. december 2., vasárnap

Protain - defining simple classes I.

We arrived to the point when you would like to create a class.

Let's start by defining a simple class, nothing fancy, just a class, with a class property and a constructor. It looks like this:
protain.namespace('hu.benqus.namespace_tutorial', function (path) {
    /**
     * defining a class - simple way
     */
    protain
        /**
         * creates a class
         */
        .class(this, 'MyFirstClass')
        /**
         * defines a property on the class
         */
        .define({
            myProperty: 'myPropertyValue'
        })
        /**
         * @constructor
         */
        .init(function (instance) {
            console.log(this); //class
            console.log(instance); //instance
        });
});

Methods:

protain.class
Method creates a class object.

Arguments:
  • namespace - a protain namespace {Object}
  • class name - name of the class in {String}
  • [node1], [node2] - node names to inherit the functionality from (same namespace) {String} (see later, in the node classes article)


protain.define
Method defines a set of properties on the class object which it was invoked on.

Arguments:
  • properties - set of attributes {Object}


protain.init
Method defines a constructor function for the class. The argument method's context (this) will be the class (!!!).
First argument will be the current instance and the remaining arguments will be the passed ones in the .create() method (see later, in the instantiation article).

Arguments:
  • constructor - a constructor function to be invoked on each instantiation {Function}


This wasn't so hard after all, right? Next one will be the "noded classes", stay tuned for more dose of protain! =)

Find Protain on GitHub: https://github.com/benqus/protain

Nincsenek megjegyzések:

Megjegyzés küldése