Question

What are the options for using the let keyword in javascript. It seems like it would be really useful.

I've seen traceur, but I'm wondering if there are any other options, so I don't have to run entire projects through it.

Is it even possible with some type of polyfill or library. Or do I basically have to wait until all the old browsers die out to use it natively...

Was it helpful?

Solution 2

There is absolutely no way to mimic / shim a keyword in ECMAscript. So you either choose to totally go with it (which might be a very bad idea at present) or you can't use it yet.

As you might know, let is part of ECMAscript Next / Harmony and at best, its just available as experimental feature in some browsers. Even if let is a very stable feature, ES.Next spec is still in flux even. Go play with it, but it really makes no sense to rely on it imho.

OTHER TIPS

Let me quote what I just read on You Don't Know JS: Scope & Closures - Appendix B: Polyfilling Block Scope

PS: This is licensed under Creative Commons CC BY-NC-ND 3.0 so it is OK to share it as long as the reference is mentioned

Large quote ahead:

Consider the following example

{
 let a = 2;
 console.log( a ); // 2
}

console.log( a ); // ReferenceError

This will work great in ES6 environments. But can we do so pre-ES6? catch is the answer.

try{throw 2}catch(a){
 console.log( a ); // 2
}
console.log( a ); // ReferenceError

Whoa! That’s some ugly, weird looking code. We see a try/catch that appears to forcibly throw an error, but the “error” it throws is just a value 2, and then the variable declaration that receives it is in the catch(a) clause. Mind: blown.

That's right, the catch clause has block-scoping to it, which means it can be used as a polyfill for block scope in pre-ES6 environments.

"But...", you say. "...no one wants to write ugly code like that!" That's true. No one writes (some of) the code output by the CoffeeScript compiler, either. That's not the point.

The point is that tools can transpile ES6 code to work in pre-ES6 environments. You can write code using block-scoping, and benefit from such functionality, and let a build-step tool take care of producing code that will actually work when deployed.

This is actually the preferred migration path for all (ahem, most) of ES6: to use a code transpiler to take ES6 code and produce ES5-compatible code during the transition from pre-ES6 to ES6.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top