maxime1992
maxime1992

Reputation: 23813

Babel ES6 - IIFE : Block only

I'm working on a project and I do have Babel 6 setup.

I thought I could use IIFE block like that :

{
   let test = 'this is a test';
}

And it would convert to :

(function(
   var test = 'this is a test';
))();

But the ouput file is :

{
   var test = 'this is a test';
}

Am I missing something here ?

Thanks

Upvotes: 1

Views: 395

Answers (1)

Bergi
Bergi

Reputation: 664936

For performance reasons, babel doesn't use IEFEs to distinguish scopes. The variables in the block will be different variables than the ones outside of the block with the same name, though:

{
   let test = 'this is a test';
}
console.log(test);

compiles to

{
   var _test = 'this is a test';
}
console.log(test);

This doesn't make any difference inside a function or a module, it would only lead to distinguishable behaviour in a global script.

Upvotes: 3

Related Questions