return when desugaring to closures

Brendan Eich brendan at mozilla.org
Thu Aug 21 16:43:25 PDT 2008


On Aug 21, 2008, at 4:05 PM, David-Sarah Hopwood wrote:

> Brendan Eich wrote:
>> The maddening mix of dynamic scope (the hopeless global top-level,
>> with, eval) and lexical scope [...]
>
> 'with' is lexically scoped.
> <http://groups.google.com/group/google-caja-discuss/browse_thread/ 
> thread/edc12e29e2cfba9f/c2ea493e83a6bf1f?lnk=gst#c2ea493e83a6bf1f>

There, you wrote

"The first example illustrates (but does not prove) that 'with' actually
provides lexical scoping, *provided* that all free identifiers in the
scope of the 'with' exist as properties of the supplied scope object. "

The *provided* is not guaranteed by 'with', so if it's not satisfied,  
then by your own argument 'with' does not provide lexical scoping.  
Perhaps you meant to write "'with' may but not must be lexically  
scoped"?

Consider this counter-example:

var opt_A = false, opt_B = 0, opt_C = ""; // defaults

function setOptions(opts) {
   with (opts) {
     if (opt_A) setOptionA(true);
     if (opt_B) setOptionB(opt_B);
     if (opt_C) setOptionC(opt_C);
   }
}
setOptions({opt_A: true, opt_C: "hi"});
setOptions({opt_B: 42, opt_C: "bye"});

I'm not recommending this kind of code, but IIRC Lars Hansen pointed  
out the pattern, based on sightings in the wild.

Is this "dynamic scope" in the classical sense? No, but it smells as  
bad and has similar undesirable properties ;-).

These old docs may be worth a read:

http://wiki.ecmascript.org/doku.php?id=clarification:lexical_scope
http://wiki.ecmascript.org/doku.php?id=proposals:reformed_with


> (Please don't interpret this as a defence of 'with'.)

No worries.

/be



More information about the Es-discuss mailing list