Last active
August 17, 2019 18:19
-
-
Save jsmanifest/d21d33c7c7fe6ea12773cc2ed25b6f00 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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