Last active
September 12, 2017 13:13
-
-
Save fernandoperigolo/7789866 to your computer and use it in GitHub Desktop.
This is a example about how implement Compose Technique in Javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 likeEletronic = 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 likeSoundReproducer = 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 | |
likeSoundReproducer = likeSoundReproducer.compose(likeEletronic) | |
// Micro System atributes and methods | |
// Not used, just example | |
var likeMicroSystem = 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, likeSoundReproducer is a composed object | |
// This line looks me strange... | |
TV = TV.compose(likeSoundReproducer); | |
// 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'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment