英文:
Passing class methods as parameters, together with arguments
问题
I am writing a program that deals with a lot of randomness, and I am in the process of implementing automated tests that measure individual run times of functions.
I have a class which defines several functions, and I am looking to write a function that will test one of these functions with given inputs X times and return total runtimes. Then I will call this function with random inputs for each function my class defines, and print the results. My class is written similar to below:
class myClass {
constructor(a) { ... }
fun1(a) { ... }
fun2(a, b) { ... }
static randomInstance { ... } // Returns a random instance of myClass
// etc.
}
And my code to test this is as follows:
function timeFunction(fun, params, it) {
let t0, t1;
let total = 0;
for (let i = 1; i <= it; i++) {
let rand = myClass.randomInstance();
t0 = performance.now();
rand.fun.apply(this, params); // I also tried fun(params)
t1 = performance.now();
total += t1-t0;
}
return total;
}
And lastly I separate the functions according to their arguments (many functions share the same set of arguments), and time each:
function myClassTest(it=50) {
// Divide functions into different arrays depending on parameters
aInputFunctions = ['fun1'];
abInputFunctions = ['fun2'];
let a = ...;
let b = ...;
for (const fun of aInputFunctions) {
const time = timeFunction(fun, [a], it);
print(it + ' iterations of ' + fun + ' took ' + time + ' milliseconds.');
print('\t Average duration of a single run: ' + time/it + '.\n');
}
for (const fun of abInputFunctions) {
const time = timeFunction(fun, [a, b], it);
print(it + ' iterations of ' + fun + ' took ' + time + ' milliseconds.');
print('\t Average duration of a single run: ' + time/it + '.\n');
}
}
However, this does not run correctly. When I run this (on Node) I get an error:
rand.fun.apply(this, params);
TypeError: Cannot read properties of undefined (reading 'apply')
How can I get this to work? What am I missing here about passing functions as arguments? I found a lot of guides about passing functions, but not much about passing class functions because who does OOP in JS, right?
And there just must be a better way to do random unit tests, but I have a (relatively) simple use case and I'm no JS expert (clearly).
英文:
I am writing a program that deals with a lot of randomness, and I am in the process of implementing automated tests that measure individual run times of functions.
I have a class which defines several functions, and I am looking to write a function that will test one of these functions with given inputs X times and return total runtimes. Then I will call this function with random inputs for each function my class defines, and print the results. My class is written similar to below:
class myClass {
constructor(a) { ... }
fun1(a) { ... }
fun2(a, b) { ... }
static randomInstance { ... } // Returns a random instance of myClass
// etc.
}
And my code to test this is as follows:
function timeFunction(fun, params, it) {
let t0, t1;
let total = 0;
for (let i = 1; i <= it; i++) {
let rand = myClass.randomInstance();
t0 = performance.now();
rand.fun.apply(this, params); // I also tried fun(params)
t1 = performance.now();
total += t1-t0;
}
return total;
}
And lastly I separate the functions according to their arguments (many functions share same set of arguments), and time each:
function myClassTest(it=50) {
// Divide functions into different arrays depending on parameters
aInputFunctions = ['fun1'];
abInputFunctions = ['fun2'];
let a = ...;
let b = ...;
for (const fun of aInputFunctions) {
const time = timeFunction(fun, [a], it);
print(it + ' iterations of ' + fun + ' took ' + time + ' milliseconds.');
print('\t Average duration of a single run: ' + time/it + '.\n');
}
for (const fun of abInputFunctions) {
const time = timeFunction(fun, [a, b], it);
print(it + ' iterations of ' + fun + ' took ' + time + ' milliseconds.');
print('\t Average duration of a single run: ' + time/it + '.\n');
}
}
However, this does not run correctly. When I run this (on Node) I get an error:
rand.fun.apply(this, params);
TypeError: Cannot read properties of undefined (reading 'apply')
How can I get this to work? What am I missing here about passing functions as arguments? I found a lot of guides about passing functions, but not much about passing class functions, because who does OOP in JS, right?
And there just must be a better way to do random unit tests, but I have a (relatively) simple use case and I'm no JS expert (clearly).
答案1
得分: 1
由于fun
是一个字符串,而不是一个函数,您不能使用fun()
。此外,您也不能使用rand.fun()
,因为这将调用名为"fun"的方法,而不使用fun
变量。
您想要的是要么
rand[fun].apply(rand, params);
要么
rand[fun](...params);
英文:
Since fun
is a string, not a function, you can't use fun()
. Also you can't use rand.fun()
, as that'll call the method named "fun" and not use the fun
variable.
What you want is either
rand[fun].apply(rand, params);
or
rand[fun](...params);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论