I wanted to get a constant variable with the global object to use it dynamically but global.myconstant doesn't work but global.myvar works.
λ node
> const myconstant = true
undefined
> global.myconstant
undefined
> var myvar = true
undefined
> global.myvar
true
> global.const
undefined
That I want to do :
const myconstant = () => { console.log("ok const"); };
var myvariable = () => { console.log("ok var"); };
const watchlist = {
myconstant: "file1.html",
myvariable: "file2.html"
};
Object.keys(watchlist).forEach((taskName) => {
try {
global[taskName](); //doesn't work with const
} catch (e) {
console.error("error", taskName);
}
});
Actual output :
error myconstant
ok var
Wanted output :
ok const
ok var
Live demo : https://repl.it/repls/SunnyScholarlyPasswords
How can I get const variable with string ? Without replace const by var.