Tiny javaScript library to build your classes inheritance in a convenient way.
This library doesn't make sense anymore, as there are classes syntax in ES6.
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());
Create base class constructor (inherited from Object).
- param {object|function} [instanceDescription]
- param {object|function} [classDescription]
- returns {constructor}
Extend your class. Every constructor, created by one of oopize methods, has method extend().
- param {object|function} [instanceDescription]
- param {object|function} [classDescription]
- returns {constructor}
Extend 3rd side class.
- param {constructor} [superConstructor]
- param {object|function} [instanceDescription]
- param {object|function} [classDescription]
- returns {constructor}
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
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
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
MIT