This is how I dynamically add a function to the window object at run-time:
(function (myPluginModuleLib, $, undefined) {
myPluginModuleLib.myModuleMethod = (varArgs) => {
console.log('Dynamic Call : ' + varArgs);
};
}(window.myPluginModuleLib = window.myPluginModuleLib || {}, jQuery));
Then I can globally call it at any time :
window.myPluginModuleLib.myModuleMethod('HELLO WORLD PARAMS!');
Is there a similar way to do this in NodeJs? I would like to add a function into memory. For exaple, in the main app.js, so that I could call it on demand, instead of always constructing it from a String:
let myModuleMethodString = 'var myModuleMethod = (varArgs) => { console.log("Dynamic Call : " + varArgs) }';
myModuleMethodString = myModuleMethodString.slice(myModuleMethodString.indexOf("{") + 1, myModuleMethodString.lastIndexOf("}"));
let myModuleMethod = new Function("varArgs", 'console.log("Dynamic Call : " + varArgs)');
try {
myModuleMethod('HELLO WORLD PARAMS!');
} catch (error) {
console.log(error);
}
Thank you all in advance.