Created
July 28, 2019 15:15
-
-
Save ANUPAMCHAUDHARY1117/8ae945e7ba1293e282061be10a54c1ea 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 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