传递类方法作为参数,以及参数。

huangapple go评论98阅读模式
英文:

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:

  1. class myClass {
  2. constructor(a) { ... }
  3. fun1(a) { ... }
  4. fun2(a, b) { ... }
  5. static randomInstance { ... } // Returns a random instance of myClass
  6. // etc.
  7. }

And my code to test this is as follows:

  1. function timeFunction(fun, params, it) {
  2. let t0, t1;
  3. let total = 0;
  4. for (let i = 1; i <= it; i++) {
  5. let rand = myClass.randomInstance();
  6. t0 = performance.now();
  7. rand.fun.apply(this, params); // I also tried fun(params)
  8. t1 = performance.now();
  9. total += t1-t0;
  10. }
  11. return total;
  12. }

And lastly I separate the functions according to their arguments (many functions share the same set of arguments), and time each:

  1. function myClassTest(it=50) {
  2. // Divide functions into different arrays depending on parameters
  3. aInputFunctions = ['fun1'];
  4. abInputFunctions = ['fun2'];
  5. let a = ...;
  6. let b = ...;
  7. for (const fun of aInputFunctions) {
  8. const time = timeFunction(fun, [a], it);
  9. print(it + ' iterations of ' + fun + ' took ' + time + ' milliseconds.');
  10. print('\t Average duration of a single run: ' + time/it + '.\n');
  11. }
  12. for (const fun of abInputFunctions) {
  13. const time = timeFunction(fun, [a, b], it);
  14. print(it + ' iterations of ' + fun + ' took ' + time + ' milliseconds.');
  15. print('\t Average duration of a single run: ' + time/it + '.\n');
  16. }
  17. }

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:

  1. class myClass {
  2. constructor(a) { ... }
  3. fun1(a) { ... }
  4. fun2(a, b) { ... }
  5. static randomInstance { ... } // Returns a random instance of myClass
  6. // etc.
  7. }

And my code to test this is as follows:

  1. function timeFunction(fun, params, it) {
  2. let t0, t1;
  3. let total = 0;
  4. for (let i = 1; i &lt;= it; i++) {
  5. let rand = myClass.randomInstance();
  6. t0 = performance.now();
  7. rand.fun.apply(this, params); // I also tried fun(params)
  8. t1 = performance.now();
  9. total += t1-t0;
  10. }
  11. return total;
  12. }

And lastly I separate the functions according to their arguments (many functions share same set of arguments), and time each:

  1. function myClassTest(it=50) {
  2. // Divide functions into different arrays depending on parameters
  3. aInputFunctions = [&#39;fun1&#39;];
  4. abInputFunctions = [&#39;fun2&#39;];
  5. let a = ...;
  6. let b = ...;
  7. for (const fun of aInputFunctions) {
  8. const time = timeFunction(fun, [a], it);
  9. print(it + &#39; iterations of &#39; + fun + &#39; took &#39; + time + &#39; milliseconds.&#39;);
  10. print(&#39;\t Average duration of a single run: &#39; + time/it + &#39;.\n&#39;);
  11. }
  12. for (const fun of abInputFunctions) {
  13. const time = timeFunction(fun, [a, b], it);
  14. print(it + &#39; iterations of &#39; + fun + &#39; took &#39; + time + &#39; milliseconds.&#39;);
  15. print(&#39;\t Average duration of a single run: &#39; + time/it + &#39;.\n&#39;);
  16. }
  17. }

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变量。

您想要的是要么

  1. rand[fun].apply(rand, params);

要么

  1. 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

  1. rand[fun].apply(rand, params);

or

  1. rand[fun](...params);

huangapple
  • 本文由 发表于 2023年3月4日 02:55:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定