javascriptfunctionecmascript-6ecmascript-harmonyarrow-functions

Immediate function using JavaScript ES6 arrow functions


Does anyone know how to write an immediate function using ES6 arrow syntax?

Here's the ES3/5 way of doing it:

(function () {
   //...
}());

I've tried the following but get an unexpected token error on the last line.

(() => {
  //...
}());

You can test this here: http://www.es6fiddle.net/hsb8bgu4/


Solution

  • From the Arrow functions examples,

    (() => "foobar")() // returns "foobar" 
    

    So, the function invocation operator should be outside.

    (() => {
      //...
    })();
    

    Sample: http://www.es6fiddle.net/hsb8s1sj/