Created
January 13, 2020 23:03
-
-
Save pfftdammitchris/85bb9a3fcffc7c0d29a685ea90249cd3 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
function Command(name, execute) { | |
this.name = name | |
this.execute = execute | |
} | |
Command.prototype.toString = function() { | |
return this.name | |
} | |
const createCommandHub = function() { | |
const commands = {} | |
return { | |
add(command) { | |
commands[command.name] = command | |
}, | |
execute(command, ...args) { | |
return commands[command].execute(...args) | |
}, | |
} | |
} | |
const cmds = createCommandHub() | |
const talkCommand = new Command('talk', function(wordsToSay) { | |
console.log(wordsToSay) | |
}) | |
const destroyEverythingCommand = new Command('destroyEverything', function() { | |
throw new Error('Destroying everything') | |
}) | |
cmds.add(talkCommand) | |
cmds.add(destroyEverythingCommand) | |
cmds.execute(talkCommand, 'Is talking a talent?') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment