I want to know if it's possible to run a function that is named a string in nodejs. All this code is running on the server side with no browser appearance at all.
Assuming I export a file test.js
with the following code
module.exports.test = function(x)
{
console.log(x*5);
}
Can I do this somehow?
main.js
imp = require('test.js');
toExecute = "test";
// somehow call imp.test using toExecute`
I want to know if it's possible to run a function that is named a string in nodejs. All this code is running on the server side with no browser appearance at all.
Assuming I export a file test.js
with the following code
module.exports.test = function(x)
{
console.log(x*5);
}
Can I do this somehow?
main.js
imp = require('test.js');
toExecute = "test";
// somehow call imp.test using toExecute`
Share
Improve this question
asked Apr 17, 2012 at 2:33
KartikKartik
9,8919 gold badges49 silver badges52 bronze badges
1 Answer
Reset to default 7Sure:
imp[toExecute](5);
Logs 25
.