paladin
Javascript Object Composition library, combine constructors and create objects as needed.
Leverage Javascript's own unique feature - prototype
- but also forget about ever typing it again!
Favour composition over inheritance
With Paladin you can put together all your pieces into objects which you can create on-the-fly.
You can re-use your components and combine them together, cache them into functions if you intend to create multiple objects of the same type, or just create it on the fly, leveraging javascript's higher-order functions.
Say you have a Car
. All Cars have an Engine
, but not all Cars have a CDPlayer
.
With Paladin you can simply do:
var simpleCar = Paladin.compose([Car, Engine]);
then pass some states when you instantiate the car:
var toyota = simpleCar({ model: 'Toyota' });
As Paladin.compose
is returning a function you can even create a simpleCar by doing:
var ford = Paladin.compose([Car, Engine])({ model: 'Ford'});
ford
is a Car, and an Engine whose methods have a common context!
Naturally an Engine can be mounted on a Plane
for example...
var boeing = Paladin.compose([Plane, Engine])({ model: '777' });
Now let's create a car with a CDPlayer, let's start it and rock our playlist:
var c = Paladin model: 'Ferrari Paladin' ;cstart next next next ;
States, init and modules
The composed function takes three optional parameters, states, init and modules.
The states object sets public members to the passed values. I.e. { name: 'joe' }
sets the public member name to joe.
With this you can attach functions as methods (see example below).
Init takes method names and arrays for parameters. I.e. { setName: ['joe'] }
calls the method setName
and passes the parameter 'joe'
.
States
Once you generated a composited function, you can create your objects by passing a states object, but you can also call the states method subsequently to the object creation. I.e.
var simpleCar = Paladin;var myCar = ;myCar;
You can also attach methods with states:
{ console;} var myBreakingCar = skid: Break ;myBreakingCar; // eeeeeeeeeeeeeeeeeeekkkk!
Init
Similarly to states, you can pass an object as the second argument which will call methods on the newly created object. The object structure is
{ methodName: [ArrayOfArguments] }
.
For example:
myCar;
Modules
Modules is interesting in two respects:
- it allows functions adopting the Module pattern to be attached to another function
- it namespaces the methods to avoid method collision/override
So let's take a look at an example to understand:
{ var capacity = 50 current = 0; return { current = 50; // tank is filled with fuel } { capacity -= 1; if current === 0 console; } ;} { var capacity = 10 current = 0; return { current = 10; // oil tank is filled with fuel } { capacity -= 1; } ;}
Both modules have a fill method so we may have run into problems...
Using the modules method the fill()
method gets namespaced.
myCar;// let's refill oil and fuelmyCarOilTank;myCarCarTank;
Composing the Composite
As the result of a composition is a function, you can reuse that to further re-compose it.
var simpleCar = Paladin;var coolCar = Paladin;var DeLoreanTimeMachine = Paladin;
Complete Example
And finally a more complete example. A javascript summary of Moorcock's Stormbringer Saga... spoiler alert
/** * A javascript version of Moorcock's Stormbringer Saga */ { thisname = '';} { this { console; return this; };} { var weapon = ''; this { weapon = weaponObject; return this; }; this { return weapon; };} // module pattern function { var skills = skillsModule; skillsModule = { skills; return skillsModuleaddSkill; } { return skills; } ; return skillsModule;} { var name = ''; this { name = weaponName; }; this { return name; };} { this { console; }} { console; return this;} { console;} // create a sword that's also a demonvar DemonSword = Paladin // Stormbringer is the coolest sword in the universe Stormbringer = {} setName : 'Stormbringer' // MournBlade is Stormbringer's twin blade MournBlade = {} setName : 'MournBlade'; // create the race of Melnibone'var Melnibonean = Paladin // create Elric, the anithero and attach the battleCast method alised as fight Elric = name: 'Elric' fight: battleCast destroy: destroyWorld // set Elric's weapon to Stormbringer setWeapon: Stormbringer // add the skills module (namespaced to skills) skills // Yrkoon is Elric's evil cousin who happens to wield MournBlade Yrkoon = name: 'Yrkoon' setWeapon: MournBlade ; // let's test everything works as supposedElric;// this is interesting because addSkill() returns addSkill so you can chain brackets// until - of course - Elric destroys the world...Elricskills'Be and Albino Prince''Destroy World';// and let's print it outconsole;Elric;