If I have
Class Car {}
do I need to wrap that with our function closure? Do var's get hoisted to window? or just to the class? What about when transpiled? Does Traceur/babel turn it into a IIFE and let's into var's?
Do I need to:
(function(){
Class Car() {}
}());
To be safe?
In JavaScript, you don't! You can write any program you want without utilizing classes or the this keyword ever! Indeed, the class syntax is somewhat new to JavaScript, and object oriented code was written with functions beforehand. The class syntax is just syntactic sugar over that function-based approach to OOP.
In the ES6 way, The function keyword is replaced with the class keyword. There's a special function named 'constructor' where the initialization of the object is done. Hence whatever is present in the body of the traditional function gets shifted to the 'constructor' function.
Multiple inheritance is not supported in ES6.
However ES6 brings class-based object oriented programming into the world of JavaScript to have more modular, clean, and understandable code. ES6 provides JS with structure when creating class-like functions by actually creating classes instead.
There does not need to be an IIFE wrapper for the class car as shown here, in fact that will create an execution context and hide the class from the rest of the page.
So you would just leave it as (not the lower case)
class Car(){}
Var is still hoisted in the same manner it was before. It will get hoisted to the top of the execution context. If the code is currently in the window's context, that is where the var will end up.
Classes are not hoisted in ECMAScript 6. So the class will be available only after it has been declared.
You can have a look at what happens when Babel transpiles your code here
You don't need to use an IIFE unless you want to hide the class, and the var Class
that gets generated is hoisted as any variable: the declaration will take place at the start, but the assignement will take place in the original line.
And yes, Babel turns let
into var
, but it also takes care scoping works as expected with extra asignements. If you just want to write ES6 code and execute it you shouldn't have to worry about these details, just follow the ES6 (ES2015) standard.
No, there's no need to wrap it like that as long as it's in code being treated as an ES6 module. Babel's default settings treat input code and files as modules. Babel does introduce functions in various places to implement correct scoping semantics, and does transform let
into var
if you have the applicable transformer enabled.
ES6 modules are always in strict mode and here's what the spec says about assignment in strict mode:
Assignment to an undeclared identifier or otherwise unresolvable reference does not create a property in the global object. When a simple assignment occurs within strict mode code, its LeftHandSide must not evaluate to an unresolvable Reference.
http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-of-ecmascript
What exactly do you mean by?:
Do var's get hoisted to [...] the class?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With