Created
July 28, 2019 14:25
-
-
Save ANUPAMCHAUDHARY1117/7c28df1764b228e196f2b6578fb3cb4e to your computer and use it in GitHub Desktop.
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
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