javascriptes6-modules

What is the name of module scope? How to refer to this scope?


We have a global object in javascript that we can refer to using window for accessing any global variable. But how do we refer to a module's scope object where all the variables in that module are stored?

I need to refer to an imported object in the module using string. If it was a global variable, I could've simply done this,

let object_I_want = window[string];

But for module, I can't find the reference for such object. I also tried using this,

const scope = this;

somefunction(){
   let object_I_want = scope[string];
}

But, this is undefined inside module context according to the specification. Is there a way to reference the module scope/module object like window?


Solution

  • There is no such object for modules. The module scope is like a function scope - an environment record that you cannot access from javascript, you can only use it to declare variables.

    There is an object that holds all the exported names of a module (which you can get by doing a namespace import * as … from …), but this doesn't seem to be what you want.