Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ANUPAMCHAUDHARY1117/8ae945e7ba1293e282061be10a54c1ea to your computer and use it in GitHub Desktop.
Save ANUPAMCHAUDHARY1117/8ae945e7ba1293e282061be10a54c1ea 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),)
}
const outFieldPlayer = (name) => {
let state = {
name,
savesMade : 0,
chances : 10,
goalsScored : 0
}
return Object.assign(state,onHit(state), onMiss(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();
console.log(DeGea.savesMade); //1
Ronaldo.hit();
console.log(Ronaldo.goalsScored); //1
console.log(Ronaldo.chances); //9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment