Proposal: opt-out local scoping

Brendan Eich brendan at mozilla.org
Thu Aug 28 12:20:52 PDT 2008


On Aug 28, 2008, at 10:36 AM, Yuh-Ruey Chen wrote:

> I've taken a look at that, and while it does address free  
> variables, I'd
> still rather not have to sprinkle redundant |var|s everywhere.

Those vars are helpful to some readers, who otherwise have to learn  
the Python/Ruby/TCL model and look for prior nonlocals. Programmers  
repeat var in unrelated paragraphs precisely to help the "spot  
reader" (including themselves, when rearranging code as Ingvar  
pointed out).

I'm not criticizing the opt-out local scoping approach. Indeed, Bill  
Joy tried too late in 1995 to get me to switch JS to use it, without  
a pragma -- just an incompatible change. At that point there was  
already too much content depending on it (Marc Andreessen made the  
same point about Mosaic and NCSA httpd already having "too many" --  
80 or so -- sites, hosting quirky HTML that motivated error- 
corrections in the Mosaic parser).

Between the utility of var for some readers and writers today, and  
the closer concept of pure lexical scope that Dave proposes, there is  
less distance than there is between today's JS and an opt-out local  
scoping regime.

This distance argument is important both for the language spec, to  
conserve concepts, and for programmers who have to read and write  
code reused (read and extended) by other programmers. Adding an opt- 
out local scoping mechanism that sees wide adoption would split the  
world of JS programs in two. I don't think it would take over  
completely, ever, so it would leave a gulf between the compatible  
scoping model and the opt-out local scoping model.

Again I'm not bike-shedding the details of your proposal or knocking  
the virtue of opt-out local scoping in other languages. I'm pointing  
out that we can't go back to 1995 and change JS to have a single  
better scoping system. Given the need for a pragmatic way to change  
the scoping model, resulting in *two* models in practice if the new  
way sees adoption, I am in favor of a shorter-distance proposal that  
does less violence to the habits of JS hackers.

Beyond this point, lexical scope actually reforms the global object  
by getting it out of the scope chain. And it allows global bindings  
that people want to count on (Date, alert, document) to become  
lexical, possibly even immutable depending on how we do it.

/be


More information about the Es-discuss mailing list