MozillaZine

Heads up on let and const changes

Talk about add-ons and extension development.
lithopsian
 
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Post Posted October 11th, 2015, 3:14 am

The behaviour of let and const keywords in javascript is gradually being changed to be compliant with ECMAscript 6. If you have been relying on non-standard behaviours of let or const in your code, it is likely to be broken by these changes. Sticky here and various blog posts out or coming:
viewtopic.php?f=23&t=2966543

In summary:
separate binding for let variables in each for loop iteration, in FF39 (for ... in and for ...of not landed yet)
let and const get new global context (affects redeclarations, including by var), now in nightly
"temporal dead zone", let and const cannot be used before they are declared, landed ages ago.

Noitidart
 
Posts: 1168
Joined: September 16th, 2007, 8:01 am

Post Posted October 11th, 2015, 10:50 am

If const was used to declare a global is that fine? Or will it need update?

lithopsian
 
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Post Posted October 11th, 2015, 12:50 pm

const can be used to declare a global. However, it will no longer be a property of the window object, but will be in its own special scope. That shouldn't affect most people. What will affect them is that redeclaration of the same global using var, which used to be entirely legal, will now throw. The most common place I've seen this is to work around multiple declarations of the type "const Cc = Components.classes", but I'm sure the ingenuity of coder folk doesn't stop there.

let is similar, but using let to declare global variables seems to be mercifully rare.

Noitidart
 
Posts: 1168
Joined: September 16th, 2007, 8:01 am

Post Posted October 13th, 2015, 10:33 pm

Thanks for that awesome info litho!

lithopsian
 
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Post Posted October 14th, 2015, 6:17 am

I said using let to declare globals was rare, but the following construct is increasingly common, often in combination with an import or require statement:
Code: Select all
let { classes: Cc, interfaces: Ci, results: Cr, utils: Cu }  = Components;


Nothing wrong with the code itself, but "let is the new var" is leading to confusion among many less experienced javascript coders. They aren't the same and one isn't a replacement for the other. It is entirely possible to only use let and forget var completely, but interchanging them randomly, or replacing existing var statements with let, will cause subtle (or not so subtle) bugs.

For some reason, const is very rare, but should probably be used more often. "const is the new let" might be a good mantra to follow. const behaves just like let except that it creates a read-only variable, and with a good code design the majority of your variables will never be changed after initial assignment. I've seen a few people recommending the use of const unless a variable is explicitly required to be mutable, but it is still rare in practice. Support has been poor, non-standard, and inconsistent until the most recent browser versions.

Noitidart
 
Posts: 1168
Joined: September 16th, 2007, 8:01 am

Post Posted October 14th, 2015, 12:38 pm

Your report of these common patterns that are broken is seriously awesome thanks man!! I was just helping someone with it and this one was so subtle,

lithopsian
 
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Post Posted October 14th, 2015, 1:23 pm

I got bitten today, after thinking I'd been smart enough to do everything right. "const Cc ..." in a sidebar xul file is now considered a redeclaration (always was, I suppose, but now it breaks) because it is in a special namespace, not on the window global. Quick fix, make them all vars :) Real fix, messy and TBD :(

Noitidart
 
Posts: 1168
Joined: September 16th, 2007, 8:01 am

Post Posted October 14th, 2015, 7:17 pm

Haha @ real fix tbd :P

Luckily I've been ok with my stuff, im all jsm's and bootstrap.js clean namespaces.

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 3 guests