我希望能够在JSON中创建一系列对象,如下所示:
const objects = {
type1: {
inputs: [value1, resultOfFunction(), value3],
typeFunction: (arg1, arg2, arg3) => {
//this does a thing with the inputs and returns an output
}
},
type2: {
inputs: [val1, val2],
typeFunction: (arg1, arg2) => {
//this does a thing with these inputs and returns an output
}
}
}
这里的要点是每个type
包含某种黑盒函数,它接受一系列参数并返回一个值。每种类型的参数数量都不同,这些参数的值也不同。我建议有一个数组inputs
,它指定要传递给每个函数的值,但我愿意接受替代。
我如何通过各自的输入一般调用这些函数? e.g。
type1.typeFunction() //obvs this doesn't work
答案 0 :(得分:2)
我如何通过各自的输入一般性地调用这些函数?
您可以使用扩展表示法指定输入:
type1.typeFunction(...type1.inputs);
...或者您在type
上定义了一个函数。
注意:
inputs: [value1, resultOfFunction(), value3]
resultOfFunction
将在处理整个对象初始值设定项时调用,而不是在调用type1.typeFunction
时调用。也许这就是你想要的,但我认为特别称它是有用的。如果不是你想要什么,那么你必须把它放在一个函数中,这样才能完成它,直到/除非调用该函数。