migrating `let' and `const'

Brendan Eich brendan at mozilla.com
Tue Nov 15 07:41:16 PST 2011


On Nov 15, 2011, at 7:17 AM, Andy Wingo wrote:

> Hello,
> 
> Do I have it right that the intention with `let' and `const' is to allow
> them in strict mode?
> 
> SpiderMonkey appears to allow them, as does V8 with --harmony, but V8
> without --harmony and JSC both abort on `const' in strict mode.

This is a gray area. To quote Waldemar from

https://mail.mozilla.org/pipermail/es-discuss/2010-October/011972.html

"It's a judgment call, and I'd take these on a case-by-case basis.  For const and let, I see little harm in browsers allowing them now in strict mode with the purely non-hoisting (C++-like) scoping rules, particularly if they complained when you accidentally declared a variable twice directly in the same block."

In order to clear the decks, some engines ban let and const in strict mode. Others (at least SpiderMonkey) allow them because (a) the engine in question supported 'let' with nearly the Harmony semantics; (b) users generally use them in a future-friendly way (no use before initialization, pretend-block-scope for const).

This doesn't matter a whole lot yet on the web, since strict mode is not being adopted aggressively, and where it has been adopted, interoperation constraints mean JS hackers have not used let at all, and (AFAIK) they've not used const either. No 'let' support apart from Firefox, and 'const' isn't in IE (and in Opera it's a 'var' synonym).

For Firefox, or really SpiderMonkey, we have had a 'let' implementation dating to the ES4 proposals era, and it's "close" but of course imperfect. We have many XUL apps and add-ons using 'let' in JS. We would like them to "use strict". So we made the call the other way from V8 and JSC.


> Likewise, none of the major engines abort on `const' in non-strict mode.
> In non-strict mode in browsers, which semantics should `const' have?
> The old one, with hoisting?

There is no well-specified "old one" -- no 'const' in ECMA-262 yet. The SpiderMonkey implementation dates back to before my hair was gray. It's goofy (have you played with it? var scope, use-before-init ok, you can re-init if in a loop) but with some discipline, programmers can use it carefully, per the Crockford rules for var (manually hoisted, no use before init).

SpiderMonkey maintainers will take the heat of changing 'let' semantics shortly, and making 'const' truly block scoped. The temporal dead zone (no use before initialization) should not break much code. We'll find out how bad a mess we've made, but again it's confined to Mozilla-only apps and add-ons. I'll report to the list on how it's going as this happens.

/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111115/a44c4e7c/attachment-0001.html>


More information about the es-discuss mailing list