Base constructor to ease prototype inheritance.
Simple example
var Objex = Animal = Objex Wolf = Animal Dog = Wolf Monkey = Animal wolf = dog = monkey = ; // check inheritanceconsole;console; console;console; console;console;console;
Don't copy / partially copy static properties of super-class
var Animal = Objex Sheep Wolf; Animalcount = 0; Animal { if Animalcount > 0 Animalcount--; }; // 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?Sheep = Animal; Sheepcount = 0; // pass array of property names as first argument// to copy certain static properties only.// Static property `count` is useless for Wolf,// but you are still able to kill it, and decrease global Animal.count!Wolf = Animal;
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; { ErrorEx__super; thisextended = true;} util;Objex;// now ErrorEx has `create` method and `__super` property MyOwnErrorEx = ErrorEx;
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 Dog = Animal {} // mixin ctor {} // more one jimmy = ; Animalprototype { console;} // mixin methodTrickyPetprototype { this;}; // copy TrickyPet.prototype methods to Dog.prototypeDog; // call copied method from the instance of Dogjimmy; // mixin methodLoudVoiceprototype { console;}; // override existing Dog prototype's method `say`Dog; jimmy;
Dynamic mixing
You can declare special static method __objexOnMixing
in the mixin, which will be called while mixin applies:
var Human = Objex {}; // mixin Humanpopulation = 1000; Humanprototype { console;}; Mutant { var doSomething = BaseprototypedoSomething; if isHungry && typeof doSomething === 'function' // extend Base method if Mutant is hungry Baseprototype { // eat anybody before doing something Basepopulation--; return doSomething; } }; // people became hungry mutantsHuman; var human = ; console; // -> 1001 human; // -> 'Doing something...' console; // -> 1000
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:
true
– default value; copy all static properties w/o existing properties overriding;false
– don't copy statis properties;{ include : [], exclude : [] }
– object with optional fieldsinclude
andexclude
which are arrays of properties' names to copy of not;Array of String
– shotcut syntax for{ include : [] }
.
wrap(ctor)
Add Objex create
and mixin
static methods to the ctor
which is not Objex successor without prototype chain modification.
mixin([options], ctor)
Mixin (copy) static and prototype methods of ctor
to the callee contructor and its prototype if they doesn't exists.
Argument options
is mostly the same as of the create
method, but the object argument can contain additional properties:
{Boolean} [override=false]
— if it equalstrue
existing methods of the callee will be overriden by mixin's methods,{Boolean} [skipDynamicMixing=false]
— if it equalstrue
__objexOnMixing won’t be called.