3 ms·
I mis-read the title as meaning a preference for using classes to handle each case over ML-style switch/case statements. The JavaScript 'object' here is called
by rgoulter 8y ago
I mis-read the title as meaning a preference for using classes to handle each case over ML-style switch/case statements.
The JavaScript 'object' here is called "map" or "dictionary" etc. in other programming languages. (And the article's technique is fine).
- DaiPlusPlus 8y agoWhile you can use any value as an object property key in JavaScript, I’m not convinced the negative performance tradeoffs are worth the supposed benefits in the article. I appreciate `switch` - with its case-fallthrough surprises beginners but most C-style languages all share this quirk - and modern-day compilers and linters will gladly remind you that usung Duff’s Device-type tricks in JS don’t work. As an aside, in C#, a string-based switch statement is actually compiled to a lazily-initialised hidden Dictionary<String,Int32> object where the values are the real integer case values - so kinda similar to the linked article - except without the runtime possibly reallocating and reinitialising the dictionary object on every invocation.
- beatgammit 8y agoThis is one nice thing about Go, which uses explicit fall through with break being there default. You can still fall through if you want. However, I've come to love pattern matching in Rust. It prevents fall through entirely, which makes it unnecessary to check if the code is doing something clever with fall through, which makes the code more simple to parse.
- Izkata 8y ago> While you can use any value as an object property key in JavaScript Strings and Symbols only. Everything else is converted to a string, which works most of the time for lookups, but can fail depending on the stringification or if you're pulling keys back out of the object.