0.0.2 • Published 8 years ago

oopize v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

oopize

Tiny javaScript library to build your classes inheritance in a convenient way.

DEPRECATED

This library doesn't make sense anymore, as there are classes syntax in ES6.

How it looks like

Create your base class

var MagicAnimal = oopize({
    say: function() {
        return 'hey, dude!';
    }
});

var a = new MagicAnimal();
console.log( a.say() ); // hey, dude!

Extend your class

var MagicDog = MagicAnimal.extend(function(parentProto) { return {
    say: function() {
        return parentProto.say.call(this)+' Ruf, ruf!';
    }
}});

var d = new MagicDog();
console.log( d.say() ); // hey, dude! Ruf, ruf!

Extend some 3rd side (not oopized) class

var MagicArray = oopize.inherit(Array, {
    add: function(el) {
        return this.push(el);
    },
    size: function() {
        return this.length;
    }
});

var a = new MagicArray();
a.add(5);
console.log(a.size());

API

Methods

oopize( instanceDescription, classDescription)

Create base class constructor (inherited from Object).

YourClass.extend( instanceDescription, classDescription)

Extend your class. Every constructor, created by one of oopize methods, has method extend().

oopize.inherit( superConstructor, instanceDescription, classDescription)

Extend 3rd side class.

Features you should know about

Method init()

Use method init() instead of constructor. This method will be called internally from returned constructor.

var Person = oopize({
    init: function(name) {
        this.name = name;
    },
    getName: function() {
        return this.name;
    }
});

var me = new Person('Roman');
console.log( me.getName() ); // Roman

Constructor inheritance

There is constructor inheritance by default. Default method init() calls parent constructor. But if you define your own init() method, you could call it or not.

var Human = oopize({
    // init method in base class
    init: function(height) {
        this.height = height || 175;
    },
    getHeight: function() {
        return this.height;
    }
});

// extend base class without init definition
var Programmer = Human.extend({});
var programmer = new Programmer();
console.log( programmer.getHeight() ); // 175

var Hobbit = Human.extend(function(parentProto) { return {
    // child class init definition
    init: function(height) {
        // call init method of parent class
        parentProto.init.call(this, height || 107);
    }
}});
var hobbit = new Hobbit();
console.log( hobbit.getHeight() ); // 107

Instance and class descriptions

If instanceDescription param is an object, all its properties will be copied to your constructor prototype.

If instanceDescription param is a function, it will be called once immediately with arguments (parentProto, constructor). This function should return object with properties for your constructor prototype. In this way you have simple access to parent prototype and current constructor. Constructor of parent class is accessible as constructor.parent

classDescription param is similar. If it's an object, all its properties will be copied directly to constructor. If it's a function, it will be called with params (parentConstructor, constructor).

classDescription properties aren't inheritable

License

MIT