aoop v1.0.3
AOOP
Augmented Object Oriented Programming This node module allow programmers to use some of Object Oriented tecniques on JavaScript (Prototype Oriented)
Getting Started
Installing
For install library is easy to install by cloning the repo. You can install trhought npm too: Local installation
npm install aoopGlobal installation
npm install -g aoopUsage
- Require
const aoop = require('aoop');
//aoop module contains this elements:
//{
// Mixin:[Function],
// Interface:[Function],
// Utils:{
// copyProto:[Function],
// copyAsProto: [Function],
// extend: [Function],
// getProto: [Function],
// isClass: [Function]
// }
//}- Mixin
Mixin(class A,class B,[... classes])
This function takes all methods from two or more classes (or constructor functions) and create another one that inherits all of them from the mixins classes. Remember that the hierarchy of inheritance goes from left to right
const { Mixin } = require('aoop');
//creating two classes A and B
//class A created as constructor function
function A(){}
A.prototype.foo = function(){console.log('foo');};
A.prototype.baz = function(){console.log('Baz from A!');};
//class B created as ES6 class
class B{
bar(){console.log('bar');}
baz(){console.log('Baz from B!');}
}
//USING MIXIN
//class C created as class
const C = new Mixin(A,B);
//class D created as ES6 class
class D extends Mixin(B,A){}
//TESTING INSTANCES
var c = new C();
var d = new D();
c.foo(); //foo
c.bar(); //bar
c.baz(); //Baz from A! (the Mixin(A,B) takes A > B hierachy)
d.foo(); //foo
d.bar(); //bar
d.baz(); //Baz from B! (the Mixin(B,A) takes B > A hierachy)- Interface
Interface(proto, constrain)
This function constrain a constructor of a class (or a particular object) to have some methods defined in the proto argument. constrain argument tell the Interface to instanciate methods from his proto to the extended class.
You can create an object from an Interface too using default methods described in the Interface proto. The Interface's instance constructor take a force argument to tell the instance to use Interface's proto defaults methods.
const { Interface, Utils } = require('aoop');
//Creating Interface that must have "bye" method
var proto = {
bye(){console.log('good bye from Hello')}
};
const Hello = new Interface(proto);
//as Function without "bye" method
function A(){Hello.call(this);}
Utils.extend(A,Hello);
//as Function with "bye" method
function B(){Hello.call(this);}
Utils.extend(B,Hello);
B.prototype.bye = function(){console.log('good bye from B');};
//as Function with defaults methods (passing "force" argument to Hello Interface)
function C(){Hello.call(this,true);}
Utils.extend(C,Hello);
//as class without "bye" method
class D extends Hello{}
//as class with "bye" method
class E extends Hello{bye(){console.log('good bye from E');}}
//as class with Hello defaults methods (passing "force" argument to Hello Interface)
class F extends Hello{constructor(){super(true);}}
var a = new A(); //throw an InterfaceError becaues is missing "bye" method
var b = new B();
var c = new C();
var d = new D(); //throw an InterfaceError becaues is missing "bye" method
var e = new E();
var f = new F();
var h = new Hello(true); //generate an Interface instance by passing true as "force" argument
b.bye(); //good bye from B
c.bye(); //good bye from Hello
e.bye(); //good bye from E
f.bye(); //good bye from Hello
h.bye(); //good bye from HelloUtils This object is a library of some tools for deal with functions.
copyProto(A,B): get and copy the prototype of B to AcopyAsProto(A,B): copy directly object B to A as prototype objectextend(A,B): extend the A function with prototype of BgetProto(A): return the correct proto of the function AisClass(A): returntrueif the A function is an ES6 class object orfalseif it is a normal JavaScript function
Contacts
If you like the project feel free to contact me on my .
Something gone wrong? Feel free to rise an issue!
Did you like this project and it was usefull? Help me improve my work: