3 ms·
I feel Proxies could be a cleaner way to handle default cases in using object literals to replace switches: const cases = new Proxy({...cases}, { get
by ulucs 7y ago
I feel Proxies could be a cleaner way to handle default cases in using object literals to replace switches:
const cases = new Proxy({...cases}, {
get: (obj, val) => obj[val] || () => {...default case}
});
It does not add any lines to the definition, and saves you from writing `|| cases[default]` all the time and bugs from forgetting to write it. Additionally, you can also use 'default' as a case.
- TheAceOfHearts 7y agoProxy is slow and unnecessary. Passing around a wrapped object that behaves in this way would be exceptionally surprising. Just move this behavior into a function if you find yourself having to repeat something many times.
- williamdclt 7y agoor write a `get` function, surely that'd be cleaner and more explicit