Equivalent of “window[“functionName”](arguments)” in server-side

If you need such a capability within a module, one hack is to store such module functions in variables within the module and then call them by accessing them from the module object properties. Example:

var x = { }; // better would be to have module create an object
x.f1 = function()
{
    console.log('Call me as a string!');
}

Now, within the module, you can call it using the value from a string:

var funcstr = "f1";
x[funcstr]();

I am learning the ropes with Node myself, the above is probably all sorts of wrong :-). Perhaps a marginally better way to write this example would be (for the module m.js):

module.exports =
{
    f1: function() { console.log("Call me from a string!"); },
    f2: function(str1) { this[str1](); }
}

Now you can:

var m = require('m.js');
m.f2('f1');

Or even just:

var m = require('m.js');
m['f1']();

FWIW!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)