48

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/

3
  • 3
    Close the grouping before calling -- })(); es6fiddle.net/hsb8ot2m Commented Mar 3, 2014 at 4:17
  • This might be a traceur bug (probably related to this issue). It works fine with babel (repl demo) Commented Aug 27, 2015 at 0:54
  • Always remember that function_name +` ()` === function_caller Commented Jun 24, 2017 at 5:33

3 Answers 3

79

From the Arrow functions examples,

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

So, the function invocation operator should be outside.

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

This way works with regular functions too, and it just makes more sense anyway - wrapping the function itself to make it an expression, then calling that expression. The fact that it also works when you wrap the whole function call is a weird quirk imo, I'm glad this doesn't work with arrows
11

Here is my demo codes!

Always remember that function_name+() === function_caller

/* ES5 */

// normal function

function abc(){
    console.log(`Hello, ES5's function!`);
}
abc();

var abc = function xyz(){
    console.log(`Hello, ES5's function!`);
};
abc();

// named function

var abc = function xyz(){
    console.log(`Hello, ES5's function!`);
}();


// anonymous function
// 1
(function(){
    console.log(`Hello, ES5's IIFE!`);
})();

// 2
(function(){
    console.log(`Hello, ES5's IIFE!`);
}());

// 3

var abc = function(){
    console.log(`Hello, ES5's function!`);
}();


/* ES6 */

// named arrow function
const xyz = () => {
    console.log(`Hello, ES6's Arrow Function!`);
};
xyz();


const xyz = (() => {
    console.log(`Hello, ES6's Arrow Function!`);
})();


// Uncaught SyntaxError: Unexpected token (

/*
const xyz = (() => {
    console.log(`Hello, ES6's Arrow Function!`);
}());
*/

// anonymous arrow function
(() => {
    console.log(`Hello, ES6's Arrow Function!`);
})();

Using ES6 Arrow Functions realize IIEF!

Immediately-invoked function expression

let x;

(x = () => {
  console.log(`ES6 ${typeof(x)}`);
})();

// ES6 function

// OR

(() => {
  console.log(`ES6 ${typeof(Symbol)}`);
})();

// ES6 function

1 Comment

"Immediately-invoked function expression" as a term describes a design pattern which has also been referred to as a "self-executing anonymous function."
0

Here's a simple example.

To define an arrow function:

const temp = (x)=> {return x+" world";}

// call it as a function
temp("hello") // output: hello world

To make an arrow function immediately invoke:

const temp = ((x)=> {return x+" world";})("hello")

// use it as a variable:
console.log(temp); // output: hello world

// a self-invoking function without params:
const temp = (()=> {return "world";})()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.