0

As I may see from grammar of ES2015 this expression is not a valid one:

() => { console.log("hello"); } ();

You need at least put parentheses:

(() => { console.log("hello"); }) ();

Chrome fails on first sample with Uncaught SyntaxError: Unexpected token (

But Babel is ok with that. Why?

Elena Vilchik
  • 1,085
  • 6
  • 11
  • Uh, no, where do you read in the grammar that it was not valid? – Bergi Dec 17 '15 at 15:02
  • Because babel transpiles your code nicely? I think it's rather like this that your chrome doesn't support ES2015 (ES6) yet, or you didn't activate the developer extensions – Icepickle Dec 17 '15 at 15:02
  • If you use the code directly in Chrome's console, it will be parsed as if it's ES5. Babel loads the .js code via ajax and transpiles it (so, Babel "understands" ES6 and translates it to ES5). – Mjh Dec 17 '15 at 15:04
  • I'm pretty sure this is a duplicate, only I cannot find it. Anyone? – Bergi Dec 17 '15 at 15:06
  • @Bergi I remember I saw this somewhere. Can't recall whether it's an issue in Babel phabricator. Still looking for it now – PSWai Dec 17 '15 at 15:07
  • @Bergi What sort of dupe are you looking for - one for ES2015 syntax, or maybe more [one like this](http://stackoverflow.com/questions/32556297/how-can-i-use-es6-syntax-such-as-let-in-chrome-console)? (or the one it itself is a dupe of) – James Thorpe Dec 17 '15 at 15:09
  • I guess better to answer it, so that in future others can be closed duplicated @Bergi – Jai Dec 17 '15 at 15:09
  • @JamesThorpe, PSWai: I mean a SO question or answer that discusses how the parenthesis are not necessary for arrow functions – Bergi Dec 17 '15 at 15:14
  • @JamesThorpe: I guess I have mixed up [this one](http://stackoverflow.com/q/33737718/1048572) with [this comment](http://stackoverflow.com/questions/32746615/namespacing-with-iife-in-es6/32750216#comment55431965_32750216) – Bergi Dec 17 '15 at 16:22
  • @PSWai: I think I found it. Is is the one you remember? – Bergi Dec 17 '15 at 16:22
  • @Bergi Not those, those two are related to async-await. I saw one that exclusively mentioning the exact issue here (or is it in my dream?). At least I can't find a dupe on SO. – PSWai Dec 18 '15 at 01:24

1 Answers1

3

But Babel is ok with that. Why?

Because Babel is buggy :-) See https://phabricator.babeljs.io/T2118 and https://phabricator.babeljs.io/T2027. If I understand correctly, it was fixed with Babel 6 - it was a quite breaking change.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375