[英]Javascript/Node.js Can I use Hashmap to Selectively Execute Functions
My application has a list of preferences, stored in a JSON file (preferences.json). 我的应用程序有一个首选项列表,存储在一个JSON文件(preferences.json)中。 I have a list of corresponding functions that I would like to selectively use based on the preferences.
我有一个列表,我希望根据偏好选择使用相应的功能。 The result of each function will be added to a report.
每个功能的结果将添加到报告中。
preferences.json looks something like this: preferences.json看起来像这样:
{
"getTemperature": true;
"getHumidity": false;
"getPrecipitation": true
}
The functions are in an module I'm importing (functions.js). 这些功能在我要导入的模块中(functions.js)。 They something like this:
他们是这样的:
var getTemperature = function(){
//retrieve temperature;
//append temperature to file;
}
var getHumidity = function(){
//retrieve humidity;
//append humidity to file;
}
var getPrecipitation = function() {
//retrieve precipitation;
//append precipitation to file;
}
What I've Tried So Far, Obviously Not Working 到目前为止,我尝试过的工作显然无法正常工作
var prefs = require('./preferences.json');
var funcs = require('./functions.js');
for (key in prefs){
if(prefs[key]) {
funcs.key(); // <- Doesn't work, b/c 'key()' isn't a function. But you get the idea.
}
}
If you know how to accomplish this, please let me know. 如果您知道如何完成此操作,请告诉我。
One idea I haven't tried yet (it would require a good deal of code re-write) is to nest the functions in a pseudo-class along with the preference dummy variables. 我尚未尝试过的一个想法(需要重写大量代码)是将函数与首选项伪变量一起嵌套在伪类中。 I'd then create an instance of the pseudo-class using the preference file.
然后,我将使用首选项文件创建伪类的实例。 My thinking is that the pseudo-class instance would have the preferences intact and I could hard code selective execution into each function (ie if(myInstance.tempBool){myInstance.getTemperature()} ).
我的想法是伪类实例将具有完整的首选项,并且我可以将代码选择性地执行到每个函数中(例如if(myInstance.tempBool){myInstance.getTemperature()})。 I'd rather iterate though, as there are many more functions and I'll probably add more in the future.
不过,我宁愿进行迭代,因为还有更多的功能,将来可能还会添加更多的功能。
Any thoughts? 有什么想法吗?
Adapting the answer from the link I put in comment above and applying it to your situation: 修改上面我在评论中添加的链接的答案,并将其应用于您的情况:
your functions.js file would contain the following: 您的functions.js文件将包含以下内容:
exports.weatherFunctions = {
var getTemperature = function(){
//retrieve temperature;
//append temperature to file;
}
var getHumidity = function(){
//retrieve humidity;
//append humidity to file;
}
var getPrecipitation = function() {
//retrieve precipitation;
//append precipitation to file;
}
}
require that in your file as you tried above: 像上面尝试的那样在文件中要求:
var prefs = require('./preferences.json');
var funcs = require('./functions.js')
from here you can loop through as you wish. 从这里您可以按需循环。
for (key in prefs){
if(prefs[key]) {
funcs.weatherFunctions[key]();
}
}
You can create an object with the names of the functions as keys and the functions as values: 您可以使用功能名称作为键和功能名称作为值来创建对象:
funcs = {
getTemperature: function(){
//retrieve temperature;
//append temperature to file;
}
getHumidity: function(){
//retrieve humidity;
//append humidity to file;
}
getPrecipitation: function() {
//retrieve precipitation;
//append precipitation to file;
}
}
If you want to put them in a separate file and use them with var funcs = require('./functions.js');
如果要将它们放在单独的文件中,并与
var funcs = require('./functions.js');
, you can put: ,您可以输入:
module.exports = funcs
Now your main file should almost work as-is. 现在您的主文件几乎应该可以按原样工作。 You'll need to change the line
funcs.key();
您需要更改
funcs.key();
to funcs[key]();
给
funcs[key]();
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.