Don't copy / partially copy static properties of super-class
var Animal = Objex.create(function() { Animal.__super.apply(this, arguments); Animal.count++; }), Sheep, Wolf;Animal.count = 0;Animal.kill = function() { if (Animal.count > 0) { Animal.count--; }};// pass `false` as first argument to prevent static fields bypass,// static method `create` will be copied anyway.// Assume, you want to count sheeps separately.// Animal.count will not be copied to Sheep.count, because it useless here.// Animal.kill will not be copied to Sheep too, aren't you want to shoot your own Sheep?
Set base constructor which differs from the Object
Suppose, you need to create Error inheritor, so your prototype chain must look like ErrorEx > Error > Object, not ErrorEx > Object.
Objex.wrap at your service!
var MyOwnErrorEx;function ErrorEx() { ErrorEx.__super.apply(this, arguments); this.extended = true;}util.inherits(ErrorEx, Error);Objex.wrap(ErrorEx, Error);// now ErrorEx has `create` method and `__super` propertyMyOwnErrorEx = ErrorEx.create(function(code) { this.code = code; MyOwnErrorEx.__super.apply(this, Array.prototype.slice.call(arguments, 1));});
Mixin
Mixin (copy) static and prototype properties from any constructor to the Objex successor, without prototype chain modification. Mixed properties will be own properties of a target object.
var Animal = Objex.create(), Dog = Animal.create(), TrickyPet = function() {}, // mixin ctor LoudVoice = function() {}, // more one jimmy = new Dog();Animal.prototype.say = function(wat) { console.log(wat);}// mixin methodTrickyPet.prototype.jumpBackward = function() { this.say('Woo-oo!');};// copy TrickyPet.prototype methods to Dog.prototypeDog.mixin
Dynamic mixing
You can declare special static method __objexOnMixing in the mixin, which will be called while mixin applies:
var Human = Objex.create(function() { Human.__super.apply(this, arguments); Human.population++; }), Mutant = function() {}; // mixinHuman.population = 1000;Human.prototype.doSomething = function() { console.log('Doing something...');};Mutant.__objexOnMixing = function(Base, isHungry) { var doSomething = Base.prototype.doSomething; if (isHungry && typeof
API
Description of the Objex and its successors static methods.
create([options], ctor)
Create successor of the callee with constructor passed as ctor argument. Argument options describes how the successor inherits statis fields: