简体   繁体   中英

How to call a function of a module dynamically in node js?

I want to do something like this.

var abc = require('./index')
var x = "someModule";
abc.x.someFunction();

So basically I want to do something like this:

abc.someModule.someFunction();

How do I achieve this?

Use bracket notation

Something like

var x = "someModule";
abc[x].someFunction();

This should work:

var abc = require('./index')
var x = "someModule";
abc.[x].someFunction();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM