开发者

Node.js binding to a method from a string

开发者 https://www.devze.com 2023-04-05 15:12 出处:网络
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?

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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消