Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Last active August 17, 2019 18:19
Show Gist options
  • Select an option

  • Save jsmanifest/d21d33c7c7fe6ea12773cc2ed25b6f00 to your computer and use it in GitHub Desktop.

Select an option

Save jsmanifest/d21d33c7c7fe6ea12773cc2ed25b6f00 to your computer and use it in GitHub Desktop.
const createWarrior = function createWarrior(name) {
let hp = 100
let battleCryInterval = 0
return {
bash: function(target) {
target.reduceHp((target.getHp() - 10))
return this
},
// Increase the wrarior's health by 60, decrementing it by 1 every second for 60 seconds
battleCry: function battleCry() {
hp += 60
battleCryInterval = setInterval(() => {
hp -= 1
}, 1000)
setTimeout(() => {
if (battleCryInterval) {
clearInterval(battleCryInterval)
}
}, 60000)
return this
},
getHp: function getHp() {
return hp
},
reduceHp: function reduceHp(count) {
hp -= count
}
}
}
const warrior = createWarrior('chris')
const otherWarrior = createWarrior('bob')
warrior
.battleCry()
.bash(otherWarrior)
.bash(otherWarrior)
.bash(otherWarrior)
.bash(otherWarrior)
.bash(otherWarrior)
const otherWarriorsHp = otherWarrior.getHp()
console.log(otherWarriorsHp) // result: 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment