Javascript node js如何自动调用node js中的任何get函数
在页面加载时,将调用在app.js页面上全局创建的get_switch()函数,然后返回一个方法。我想执行这些返回方法 demo.jsJavascript node js如何自动调用node js中的任何get函数,javascript,node.js,Javascript,Node.js,在页面加载时,将调用在app.js页面上全局创建的get_switch()函数,然后返回一个方法。我想执行这些返回方法 demo.js const return_functions = get_switch('BTC'); function get_btc() { console.log('btc'); } function get_bch() { console.log('bch'); } app.js global.get_switch=function(coin
const return_functions = get_switch('BTC');
function get_btc()
{
console.log('btc');
}
function get_bch()
{
console.log('bch');
}
app.js
global.get_switch=function(coin_name){
switch(coin_name){
case 'BTC':
return 'get_btc()';
break;
case 'BCH':
return 'get_bth()';
break;
default:
console.log('default');
}
}
如上面的例子所示,我在get_开关中通过了BTC。该函数返回get_btc()函数。所以我想同时调用get_btc函数
如果这样做不可行,请用您的想法指导我,并建议我如何做到这一点。您可以直接调用函数
function get_btc() {}
function get_bch() {}
global.get_switch = function (coin_name) {
switch(coin_name){
case 'BTC':
return get_btc();
break;
case 'BCH':
return get_bch();
break;
default:
console.log('default');
}
}
您可以将所有函数存储到一个类中,然后使用货币名称调用它们 我还添加了一个tho,即使用枚举来处理您的货币
class CurrencyHandlingClass{
//将所有货币类型存储到枚举中
静态获取货币类型(){
返回{
BTC:‘BTC’,
BTH:“BTH”,
};
}
//获取Btc的方法
静态getBtc(){
console.log('btc');
}
//获取银行控股公司的方法
静态getBth(){
console.log('bth');
}
}
//这里是您要调用的函数的名称
const currencyName1=CurrencyHandlingClass.CURRENCY\u TYPES.BTC;
const currencyName2=CurrencyHandlingClass.CURRENCY\u TYPES.BTH;
//执行该方法
CurrencyHandlingClass[`get${currencyName1}`]();
CurrencyHandlingClass[`get${currencyName2}`]()代码>但是get_开关函数是从另一个路由调用的。所以我必须在那里使用return函数。