appolo-class

    0.2.15 • Public • Published

    Appolo Class Build Status Dependencies status

    Simple and powerful class system for node js

    Installation

    npm install appolo-class --save

    Usage

    creating simple class

    var Rectangle = Class.define({
        area: function () {
            return 25;
        }
    });
     
     var rectangle = new Rectangle();

    creating class with constractor

    var Rectangle = Class.define({
        constructor: function (width, height) {
            this.height = height;
            this.width = width;
        },
     
        area: function () {
            return this.width * this.height;
        }
    });
     
    var rectangle = new Rectangle(5, 5);
     
    console.log(rectangle.area()) // 25

    Inheritance

    var Rectangle = Class.define({
     
        constructor: function (width, height) {
            this.height = height;
            this.width = width;
        },
        area: function () {
            return this.width * this.height;
        }
    });
     
    var Square = Class.define({
     
        $config: {
            extends: Rectangle
        },
        
        constructor: function (side) {
            this.callParent(this, side, side);
        }
    });
     
    var square = new Square(6);
     
    console.log(square.area()) // 36

    you can also use Class.define fucntion

    var Rectangle = Class.define({
     
        constructor: function (width, height) {
            this.height = height;
            this.width = width;
        },
        area: function () {
            return this.width * this.height;
        }
    });
     
    var Square = Rectangle.define({
     
        constructor: function (side) {
            this.callParent(side, side);
        }
    });
     
    var square = new Square(6);
     
    console.log(square.area()) // 36

    Call Parent

    use this.callParent function to call the parent method

    var Rectangle = Class.define({
     
        constructor: function (width, height) {
            this.height = height;
            this.width = width;
        },
     
        area: function () {
            return this.width * this.height;
        }
    });
     
    var Square = Rectangle.define({
     
        constructor: function (side) {
            this.callParent(side, side);
        }
    });
     
    var Cube = Square.define({
     
        constructor: function (side) {
            this.callParent(side);
     
            this.side = side;
        },
     
        area: function () {
            return 6 * this.callParent()
        },
     
        volume: function () {
            return Math.pow(this.side,3);
        }
    });
     
    var cube = new Cube(5);
     
    console.log(cube.area()) //150;
    console.log(cube.volume()) //125;

    Statics

    var Rectangle = Class.define({
        $config: {
            statics: {
                MIN_SIDE: 1,
                MAX_SIDE: 150
            }
        },
     
        constructor: function (width, height) {
            this.height = height;
            this.width = width;
        },
     
        area: function () {
            return this.width * this.height;
        }
    });
     
     
    console.log(Rectangle.MIN_SIDE) //1;
     
    var rectangle = new Rectangle(5, 5);
    console.log(rectangle.MIN_SIDE.should.equal(1)) //1;

    Mixin

    used to add protoype functions from other classes

    var Events = Class.define({
        bind: function (event, fn) {
            return true;
        },
        unbind: function (event, fn) {
            return true;
        }
    });
     
    var Rectangle = Class.define({
        $config: {
            mixins: [Events]
        },
     
        constructor: function (width, height) {
            this.height = height;
            this.width = width;
        },
     
        area: function () {
            return this.width * this.height;
        }
    });
     
    var rectangle = new Rectangle(5, 5);
    rectangle.bind('test',function(){})
    rectangle.unbind('test',function(){})

    Namespace

    create class on the global scope

    Class.define('Test.Position.Base', {
     
        constructor: function (symbol, amount, side) {
     
            this.symbol = symbol;
            this.amount = amount;
            this.side = side;
        }
    });
     
    Position.define('Test.Position.Long', {
     
        constructor: function (symbol, amount) {
            this.callParent( symbol, amount, 2);
        }
    });
     
    Position.define("Test.Position.Short", {
     
        constructor: function (symbol, amount) {
     
            this.callParent(symbol, amount, 2);
        }
    });
     
    var short = new Test.Position.Short();
    var long = new Test.Position.Long();

    Contractor Name

    you can define the class name

    var Position = Class.define({
     
        constructor: function (symbol, amount, side) {
     
            this.symbol = symbol;
            this.amount = amount;
            this.side = side;
     
        }
    });
     
    var Long = Position.define({
        $config: {
            name: "long"
        },
     
        constructor: function (symbol, amount) {
            this.callParent( symbol, amount, 2);
        }
    });
     
    var Short = Position.define({
        $config: {
            name: "short"
        },
        constructor: function (symbol, amount) {
     
            this.callParent(symbol, amount, 2);
        }
    });
     
     
    var short = new Short();
    var long = new Long();
     
    consloe.log(short.constructor.name) // short
    console.log(long.constructor.name) //long;

    Members (Properties)

    define properties with getter and setter functions. if a property in defined to be a member and a getter or setter is not implemented then erro will be trown

    var Rectangle = Class.define({
        $config: {
            members: ['area','name']
        },
        
        constructor: function (width, height) {
            this.height = height;
            this.width = width;
        },
     
        getArea: function () {
            return this.width * this.height;
        },
        getName:function(){
            return this._name;
        },
         setName:function(value){
            this._name = value;
        }
    });
     
    var rectangle = new Rectangle(5, 5);
    console.log(rectangle.area) //25;
     
    rectangle.area = 10 // error not implemented
     

    Plugins

    you can your custom plugins to the class system by adding callback function the function will be called with 3 arguments

    • config - config object of the class
    • klass - class referance
    • parent - parent class referance
    Class.addPlugin(function(config,klass,parent){
        //do something
    });

    Tests

        grunt test

    License

    The appolo class library is released under the MIT license. So feel free to modify and distribute it as you wish.

    Install

    npm i appolo-class

    DownloadsWeekly Downloads

    29

    Version

    0.2.15

    License

    Mit

    Last publish

    Collaborators

    • shmoop207
    • antonzy