Skip to content

Instantly share code, notes, and snippets.

@pfftdammitchris
Created January 13, 2020 23:03
Show Gist options
  • Save pfftdammitchris/85bb9a3fcffc7c0d29a685ea90249cd3 to your computer and use it in GitHub Desktop.
Save pfftdammitchris/85bb9a3fcffc7c0d29a685ea90249cd3 to your computer and use it in GitHub Desktop.
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