For special purposes in CoffeeScript, you can use the existential operator:
name = obj?.props?.name
This leads to a rather long block of code that checks that objit is propsdefined.
name = typeof obj !== "undefined" && obj !== null ?
(_ref2 = obj.props) != null ?
_ref2.name : void 0 : void 0;
Consider a more complex, destructive purpose:
{name: name, emails: [primary], age: age} = Person.get(id)
If the object does not contain a property emails, this code will call TypeError. Is there a way to use an existential operator with such destructuring assignments?
This is the best alternative that I have so far:
{name: name, emails: emails, age: age} = Person.get(id)
primary = emails?[0]
source
share