ES Modules: suggestions for improvement

Claus Reinke claus.reinke at talk21.com
Wed Jun 27 14:25:57 PDT 2012


>> let * = Math;
>>
>> This is dynamic scoping. The difference between import * and let * is 
>> that
>> the former is statically scoped, and the latter is dynamically scoped.
>
> I'm sorry, I'm not entirely sure what static scoping means in the context
> of JavaScript. Could you clarify? Does it mean that it's only applicable 
> in
> the context of the current file, module, domain or something like that?

Perhaps I can answer this, though I'm not involved with ES Modules.

"Static" scoping means that scoping does not depend on runtime
behavior. If "Math" is the Module object for the module "Math", then
we have a "dynamic" object (dependent on runtime behavior). If let
destructuring were to support "*", the variables in scope after such
a statement would depend on the properties of a dynamic object,
so scoping would no longer be static:

    let * = flip_coin() ? {sin: .., cos: .. } : {apples: .., bananas: ..}
    console.log( sin(3.14) ); // is 'sin' bound or not?

That doesn't mean that "static" vs "dynamic" is a clear-cut distinction
in JS, where code can be constructed and loaded at runtime. But that
just makes it even more important to have clear phase separations,
so that one can tell when the static and dynamic phases of each piece
of code begin (construction followed by static followed by dynamic).

Language designs that try to unite dynamic modules with static
scoping are a well-known case of needing very careful design.
"import *" is barely on the safe side, if done right, "let *" is
just on the wrong side of this dangerous border.

Hope this helps,
Claus
 



More information about the es-discuss mailing list