简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM