paladin

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:

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.compose([Car, Engine]);

var myCar =newsimpleCar();

myCar.states({ model:'My Car'});

You can also attach methods with states:

functionBreak(){

console.log('Eeeeeeeeeeekkkkk!');

}

var myBreakingCar =newsimpleCar({ skid: Break });

myBreakingCar.skid();// 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.init({ start:[], setModel:['Ferrari Paladin']});

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:

functionCarTank(){

var capacity =50,

current =0;

return{

fill:function(){

current =50;// tank is filled with fuel

},

consume:function(){

capacity -=1;

if(current ===0){

console.log('We\'re out of fuel!');

}

}

};

}

functionOilTank(){

var capacity =10,

current =0;

return{

fill:function(){

current =10;// oil tank is filled with fuel

},

consume:function(){

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.modules([CarTank, OilTank]);

// let's refill oil and fuel

myCar.OilTank.fill();

myCar.CarTank.fill();

Composing the Composite

As the result of a composition is a function, you can reuse that to further re-compose it.

var simpleCar =Paladin.compose([Car, Engine]);

var coolCar =Paladin.compose([simeplCar, CDPlayer]);

var DeLoreanTimeMachine =Paladin.compose([coolCar, TimeMachine]);

Complete Example

And finally a more complete example. A javascript summary of Moorcock's Stormbringer Saga... spoiler alert