Skip to content

Instantly share code, notes, and snippets.

@ANUPAMCHAUDHARY1117
Last active July 28, 2019 16:27
Show Gist options
  • Save ANUPAMCHAUDHARY1117/910ec10070b72b7e36619c7e9d313e4a to your computer and use it in GitHub Desktop.
Save ANUPAMCHAUDHARY1117/910ec10070b72b7e36619c7e9d313e4a to your computer and use it in GitHub Desktop.
const onHit = (state) => ({
hit : () => {
if(state.chances === 0){
return console.log("Game is over");
}
state.chances -= 1;
state.goalsScored += 1;
}
});
const onMiss = (state) => ({
miss : () => {
if(state.chances === 0){
return console.log("Game is over");
}
state.chances -= 1;
}
});
const onSave = (state) => ({
save : () => {
state.name = "Oblak"
state.savesMade += 1;
}
});
const player = (name) => {
let state = {
name,
chances : 10,
goalsScored : 0
}
return Object.assign(state,onHit(state), onMiss(state),)
}
//This can be easily changes and no tight coupling is there
const outFieldPlayer = (name) => {
let state = {
name,
savesMade : 0,
}
return Object.assign(state,onSave(state))
}
var Ronaldo = Object.assign(Object.create(player()), {name : "Cristiano Ronaldo"});
var DeGea = Object.assign(Object.create(outFieldPlayer()), {name : "De Gea"});
DeGea.save();
DeGea.hit(); //Error => DeGea.hit is not a function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment