2012. december 14., péntek

Protain - defining (extending) simple classes II.

Protain also supports a "classical" syntax to create prototype chains - it doesn't look like the syntax in Java, C++ or C# or etc., but at least it is similar. So, let's do a class named Person with some methods - of course we are inside a scope of a namespace, which is refered with this:
/**
 * Person Class
 */
protain
    .class(this, 'Person')
    /**
     * @constructor
     * @param instance {Object} the new instance
     * @param name {String} the name of the instance
     */
    .init(function (instance, name) {
        instance.setName(name);
    })
    //class properties
    .define({
        /**
         * sets the name property of the context
         */
        setName: function (name) {
            //type check
            if (typeof name !== 'string' && name === '') {
                throw new TypeError('Invalid argument!');
            }

            //set the name
            this.name = name;

            //return the context
            return this;
        },
        /**
         * returns the name property of the context
         */
        getName: function () {
            return this.name;
        }
    });
Now, that we have a base class, we can extend from it using the .extend() method and .build() to build the instance with the inherited functionality:
/**
 * AgingPerson
 */
protain
    .class(this, 'AgingPerson')
    /**
     * setting up inheritance
     */
    .extend(this, 'Person')
    /**
     * @constructor
     * @param instance {Object} the new instance
     * @param name {String} the name of the instance
     * @param age {Number} the age of the instance
     */
    .init(function (instance, name, age) {
        /**
         * .build() will initialise the instance based on the inheritance and the arguments passed
         */
        this.build(arguments);

        instance.setAge(age);
    })
    .define({
        /**
         * returns the age property of the context
         */
        getAge: function () {
            return this.age;
        },

        /**
         * sets the age property of the context
         */
        setAge: function (age) {
            if (typeof age !== 'number') {
                throw new TypeError('Invalid argument!');
            }

            //set the age
            this.age = age;

            //return the context
            return this;
        }
    });
The .extend() method takes two parameters:
  • namespace - the namespace to register the extend to
  • className - the name of the class to extend

When you extend from a class, you will have to use the this.build(arguments); method inside the constructor to build the instance based on the inheritance. This method calls the base class's constructor method, just like the super() method in Java.

And then you can use the .create() method for instantiating:
var AgingPerson = protain.class(this, 'AgingPerson');

var agingPerson = AgingPerson.create('benqus', 26);
Well, I should write some more example on GitHub but so far you are good to go with Protain. =)

I hope you enjoyed these tutorials! Any question are welcome! =)

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

Nincsenek megjegyzések:

Megjegyzés küldése