On Wed, Jan 26, 2011 at 2:04 PM, James Burke <jrbu...@gmail.com> wrote: > CommonJS Modules 1.1 allows this kind of construct in module code: > > var a; > if (someCondition) { > a = require("a1"); > } else { > a = require("a2"); > } > > and the module "a1" is not actually evaluated until execution reaches > the a = require("a1") call. > > 1) Could something like this work in Simple Modules? If so, what would > be the syntax for it?
You can use module loaders to do exactly this (I believe, based on my understanding of CommonJS). It would look like: var ml = ... the desired module loader ... var a; if (someCondition) { a = ml.load("a1"); } else { a = ml.load("a2"); } This produces a module instance object, bound to |a|. It doesn't, however, allow you to import from |a|, since nothing is statically known about what the exports of |a| are. > 2) What are the design decisions behind only allowing "module" and > "use" at the top level of a module? Modules are a statically scoped construct, and the implementation and the programmer can both statically tell where variables come from. This prevents modules from being dynamically created, except in the context of module loaders, as seen above. -- sam th sa...@ccs.neu.edu _______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss