最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs execute exported function from string - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 7

Sure:

imp[toExecute](5);

Logs 25.

发布评论

评论列表(0)

  1. 暂无评论