How to access the ES6 module variable by name (from the module itself)

In a webpack environment, I have an ES6 module that defines a variable:

let my_var1 = 0
// other vars follow

Then in the same module I have a setter function for this variable, but I would like to build it like this:

export const set_var(name, value) => {
    // set "value" as new value for "name" variable of this module
}

What I want to do is reference the variable inside the module by name , so from outside, call something likeset_var('my_var1', 5)

Is there any way to access the "module" object? Something like a window object, but for a module.

+4
source share
2 answers

No, It is Immpossible.

, .
, let .

0

, (aka my_var1). my_var1 getter:

let moduleObject = {
    my_var1: 0,
    my_var2: 5,
    ...
}

export const set_var = (varName, value) => moduleObject[varName] = value
export const get_var = (varName) => moduleObject[varName]
0

All Articles