Created
November 15, 2019 21:44
-
-
Save deadkff01/b26ce5063408904490da536399797ed9 to your computer and use it in GitHub Desktop.
Using an object as a switch case with javascript
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
// obj literals with javascript | |
const actions = { | |
'sayHello': name => `hello my friend ${name}`, | |
'typeOf': value => typeof value, | |
'onlyString': 'simple string', | |
'default': null | |
} | |
const reducer = (action, payload) => { | |
if(!actions[action]) return actions['default'] | |
if(typeof actions[action] === 'function') | |
return actions[action](payload) | |
return actions[action] | |
} | |
reducer('sayHello', 'deadkff01') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment