Support () => {}() syntax?

Olivier Lalonde olalonde at gmail.com
Thu Sep 29 22:05:28 UTC 2016


I occasionally write IIFE to avoid introducing `let` variables, e.g.:

```
const thumb = ({ width, height }) => {
  const { w, h } = (() => {
    // if either width or height is set, don't set any default
    if (Number.isInteger(width) || Number.isInteger(height)) {
      return { w: width, h: height }
    }
    return { w: 400, h: 400 }
  })()
  // ...
}
```

One annoyance is that arrow functions must be wrapped inside parens in
order to be called. In other words, you can do `(() => {})()` but not `()
=> {}()`. The latter syntax is less verbose but is not supported.

I understand that the short style, e.g. `val => val + 1()` or `val =>
(fn())()` would be ambiguous but I don't believe the `() => {}` style would
be. Of course it gets a bit ambiguous when you get `() => () => {}()` but
that form isn't very common for IIFE.

Just wondering if this has been discussed before.

-- 
- Oli

Olivier Lalonde
http://www.syskall.com <-- connect with me!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160929/b1e5217d/attachment.html>


More information about the es-discuss mailing list