[英]How can I make an array of object in my function parameter call a function for each object?
I created the following checkGrid
function which takes a count and then another two parameters. 我创建了以下checkGrid
函数,该函数先进行计数,然后再进行另外两个参数的处理。 It's a solution which hardcodes in the selectOption calls and is not very flexible: 这是一个在selectOption调用中进行硬编码的解决方案,并且不够灵活:
checkGrid = function(expectCount, typeParam, selectParam) {
it('Check', function () {
selectOption('examTypeSelect', typeParam).click();
selectOption('examStatusSelect', selectParam).click();
});
}
checkGrid(10, '*', '*');
Now I would like to make this more flexible so that it accepts arguments for any number of selectOption functions. 现在,我想使其更加灵活,以便它接受任意数量的selectOption函数的参数。 So I was thinking of something that I could call like this. 所以我在想可以这样称呼的东西。 HEre what I would need is for the checkGrid
function to call the selectOption
three times: 在这里,我需要的是checkGrid
函数调用selectOption
三次:
checkGrid(10, [{'examTypeSelect','*'}
{'examStatusSelect', '*'},
{'abcSelect','29'}]);
How could I take an array object that's the second parameter for checkGrid and make it so that it calls any number of selectOption
functions depending on how many elements there are in the array? 我怎样才能接受作为checkGrid的第二个参数的数组对象,并使其根据数组中有多少个元素来调用任意数量的selectOption
函数?
By using a loop over the array and .apply
to call each function with a variable number of arguments. 通过在数组上循环并使用.apply
调用具有可变数量参数的每个函数。
checkGrid = function(expectCount, stuff) {
it('Check', function () {
for (var i = 0; i < stuff.length; ++i) {
selectOption.apply(this, stuff[i]).click();
}
});
}
You would call this not with your proposed syntax (which is invalid), but like 您不会使用建议的语法来调用它(这是无效的),而是
checkGrid(10, [
['examTypeSelect','*'],
['examStatusSelect', '*'],
['abcSelect','29','and','perhaps','more','parameters']
]
);
Finally, it's unclear what expectCount
is supposed to do but I left it there because your original code presumably does something with it. 最后,目前尚不清楚expectCount
应该做些什么,但是我把它留在那里了,因为您的原始代码大概可以对它做些什么 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.