Creates prototype chain with the properties from the provided objects, by (shallow) copying own properties from each object onto respective elements in the chain.
var mixly =require('mixly');
var o1 ={O1:true, commonThing:'o1'}
, o2 ={O2:true, commonThing:'o2'}
, o3 ={O3:true, commonThing:'o3'}
;
// (o1) -> (o2) -> (o3)
var o0 =mixly(o1, o2, o3);
assert.notStrictEqual(Object.getPrototypeOf(o0), o1,'Object `o0` does not have prototype set to object `o1`');
assert.notStrictEqual(Object.getPrototypeOf(o1), o2,'Object `o1` does not have prototype set to object `o2`');
assert.notStrictEqual(Object.getPrototypeOf(o2), o3,'Object `o2` does not have prototype set to object `o3`');
assert.strictEqual(o0.O1,true,'copied properties from the first object');
assert.strictEqual(o0.O2,true,'has access to the properties of the second object');
assert.strictEqual(o0.O3,true,'has access to the properties of the third object');
assert.strictEqual(o0.commonThing,'o1','shared properties from first object "win"');
assert.strictEqual(Object.getPrototypeOf(o0).commonThing,'o2','has access to the shared properties of the second object');
assert.strictEqual(Object.getPrototypeOf(Object.getPrototypeOf(o0)).commonThing,'o3','has access to the shared properties of the third object');
assert.strictEqual(o0.hasOwnProperty('commonThing'),true,'has shared properties from first object as own');
assert.strictEqual(o0.hasOwnProperty('O1'),true,'has shared properties from first object as own');
assert.strictEqual(o0.hasOwnProperty('O2'),false,'does not own properties from the second object');
assert.strictEqual(o0.hasOwnProperty('O3'),false,'does not own properties from the third object');
Extends target class with superclass, assigns superclass prototype as prototype of the target class and adds superclass itself as __proto__ of the target class, allowing "static" methods inheritance.
var extend =require('mixly/extend');
functionF1()
{
F1.super_.apply(this,arguments);
this.p0+='d2';
}
F1.prototype.p1='f1';
F1.static1=true;
functionF2()
{
this.p0='r2';
}
F2.prototype.p2='f2';
F2.static2=true;
// F1 -> F2
extend(F1,F2);
assert.strictEqual('p1'inF1.prototype,false,'original prototype is gone');
assert.strictEqual('p2'inF1.prototype,true,'replaced with new prototype');
assert.strictEqual(F1.static1,true,'original static property is accessible');
assert.strictEqual(F1.static2,true,'extended static property is accessible');
// new instance
var f1 =newF1();
assert.strictEqual(f1.p0,'r2d2','constructor executes provided super constructor');
Assigns prototype from the superclass to the target class, compatible with node's builtin version (util.inherit), but browser-friendly (without browserify magic, i.e. works with other packagers).
var inherit =require('mixly/inherit');
functionChild(){Parent.call(this);}
functionParent(){}
// Child -> Parent
inherit(Child, Parent);
// create instance
var child =newChild();
assert.strictEqual(child.constructor.super_, Parent,'child has reference to the Parent constructor');
assert.strictEqual(Object.getPrototypeOf(Object.getPrototypeOf(child)),Parent.prototype,'child has Parent in the prototype chain');
assert.strictEqual(child instanceof Child,true,'child instance of Child');
assert.strictEqual(child instanceof Parent,true,'child instance of Parent');