Is IIFE required for a class in ECMAScript / Javascript 6?

If i have

Class Car {} 

Do I need to wrap this with our close function? Expand var in a window? or just for class? What about blocking? Does Traceur / babel include in IIFE and lets in var?

If necessary:

 (function(){ Class Car() {} }()); 

To be safe?

+6
source share
3 answers

You can take a look at what happens when Babel converts your code here.

You do not need to use IIFE if you do not want to hide the class, and the var Class that is generated is raised like any variable: the declaration will be executed from the very beginning, but the assignment will take place in the original line.

And yes, Babylon turns let into var , but also works on what is expected with additional entries. If you just want to write ES6 code and execute it, you don’t have to worry about these details, just follow the ES6 standard (ES2015).

+1
source

Class wallpaper does not have to have an IIFE shell, as shown in the figure, in fact it will create an execution context and hide the class from the rest of the page.

So you just leave it as (not lowercase)

 class Car(){} 

Var still rises as before. It will be raised to the top of the execution context. If the code is currently in the context of the window, then this will be where var will be.

Classes are not loaded in ECMAScript 6. Thus, a class will be available only after it is declared.

+2
source

No, there is no need to wrap it this way while it is being processed as ES6 module in the code. By-default settings process input code and files in the form of modules by default. Babel really introduces functions in different places to implement the correct semantics of semantics and converts let to var if you have the appropriate transformer turned on.

ES6 modules are always in strict mode, and this is what they say in strict mode:

Assigning an undeclared identifier or otherwise an unsolvable reference does not create a property in the global object. When simple assignment occurs in strict mode code, its LeftHandSide should not evaluate an unsolvable reference.

http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-of-ecmascript

What do you mean?:

Do you have a var for the [...] class?

+1
source

All Articles