I'm not expert at all in Javascript and node.js If I want to access to a method that is contained into a string, What should I do? Is that possible?
For instance:
function bindJS(method, path){
var js = require(path+".js");
}
and the method I'd like to get is: js.what's_inside_开发者_开发问答method
Any idea how to do that?
Thanks!
Is method a property of js
? Can you use js[method]()
?
The only way to do this is with eval which is potentially unsafe if your data comes from a non-trusted source (unless it's hard-coded into the code, it's an untrusted source). So, lots of red flags, but this should work:
function bindJS(method, path){
var js = require(path+".js");
func = eval(method);
func();
}
精彩评论