Well, at ES6 we have a definition of block scope with let
statement. It's good and we can use it at modern browsers.
What about old ones? How to support it? For example IE8, IE9. Is there any standard trick to achieve this?
Well, at ES6 we have a definition of block scope with let
statement. It's good and we can use it at modern browsers.
What about old ones? How to support it? For example IE8, IE9. Is there any standard trick to achieve this?
You can see the current compatibility here: https://kangax.github.io/compat-table/es6/#let
Notice that as no browser currently support it, you can use polyfills or compilers like https://google.github.io/traceur-compiler
From most cases, using var
would be fine. Else you will have to encaspulate in some closure to simulate it.
Use something like babel.js
Babel is a JavaScript compiler. Use next generation JavaScript, today.
Babel will 'transpile' your ES6 to ES5 - you may have to limit yourself to a subset of ES6 to support IE < 9, but from what I can tell, let
is OK.
Block-level scopes in ES6
heavily modifies the way the interpreters defines scopes, and evaluates code. There is no way to polyfill this behaviour on older browser.
The only solution for using ES6
features is to use an intermediate language such as Typescript
which is EcmaScript
compliant (you can use ES6
feature in it, including let
and const
).
You will then need to compile the Typescript code into Javascript code when building your application.
See also : What's new in Typescript 1.6