I'm trying to use the good parts of Javascript and dont follow the classical inheritance. I studied and created this example of compose in Javascript, https://gist.github.com/fernandoperigolo/7789866.
My question is. You think this example have a good implementations of composition? Can i go ahead with it? If not, What you suggest?
(function(){
// Adding compose to Function prototype
Function.prototype.compose = function(argFn) {
var fnToCall = this;
return function() {
// This line is really complex and i don't know exactly what this line do
return fnToCall.call(this, argFn.apply(this,arguments));
}
}
// Eletronic atributes and methods
var asEletronic = function(){
this.voltage = '220v';
this.power = false;
this.switchPower = function(){
if (this.power === true) {
this.power = false;
console.info('Eletronic are off now.');
}else{
this.power = true;
console.info('Eletronic are on now.');
};
}
return this;
};
// Sound Reproducer atributes and methods
var asSoundReproducer = function(){
this.watts = 60;
this.playSound = function(sound){
console.info('Playing the sound: '+sound);
};
return this;
};
// All sound reproducer is a eletronic
// Composing first level
asSoundReproducer = asSoundReproducer.compose(asEletronic)
// Micro System atributes and methods
// Not used, just example
var asMicroSystem = function(){
this.cd = false;
this.mp3 = true;
return this;
};
// TV atributes and methods
var TV = function(){
this.pol = 42;
this.hdmi = true;
return this;
};
// Compose a TV with full resources
// Compose second level, asSoundReproducer is a composed object
// This line looks me strange...
TV = TV.compose(asSoundReproducer);
// Create a new TV
var myTV = new TV();
// Looking my TV
console.log(myTV);
// Call a Eletronic method
myTV.switchPower();
// Call a Sound Reproducer method
myTV.playSound('guitar.mp3');
})();