5 ms·
As much as Coffeescript gets maligned, its best feature (that hasn't been replicated, not even here) was the existential operator: friends = user?.friends?[0
by morley 10y ago
As much as Coffeescript gets maligned, its best feature (that hasn't been replicated, not even here) was the existential operator:
friends = user?.friends?[0]?.friends
It would even work properly with functions:
friends = getFriends?()
Which compiles to:
friends = typeof getFriends === "function" ? getFriends() : void 0;
`idx` is not quite the existential operator, but it seems cleaner then `_.get`'s string representation of your path.
- lgas 10y agoFWIW, the existential operator is not unique to CoffeeScript, e.g. I'm fairly certain Cobra had it before CoffeeScript was created.
- evv 10y agoThe existential operator has been introduced as an ECMAScript proposal[1], so it will probably be included in future versions of JavaScript. The `idx` function is just a temporary workaround. When it gets added to the language, we can codemod away all usage of `idx` to use the question-mark syntax. [1] https://github.com/claudepache/es-optional-chaining https://github.com/claudepache/es-optional-chaining