Skip to content

Instantly share code, notes, and snippets.

@ANUPAMCHAUDHARY1117
Created July 28, 2019 14:25
Show Gist options
  • Save ANUPAMCHAUDHARY1117/7c28df1764b228e196f2b6578fb3cb4e to your computer and use it in GitHub Desktop.
Save ANUPAMCHAUDHARY1117/7c28df1764b228e196f2b6578fb3cb4e 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 player = (name) => {
let state = {
name,
chances : 10,
goalsScored : 0
}
return Object.assign(state,onHit(state), onMiss(state))
}
var Ronaldo = Object.assign(Object.create(player()), {name : "Cristiano Ronaldo"});
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