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

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

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 &lt;= 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 = [&#39;fun1&#39;];
  abInputFunctions = [&#39;fun2&#39;];
  

  let a = ...;
  let b = ...;
  for (const fun of aInputFunctions) {
    const time = timeFunction(fun, [a], it);
    print(it + &#39; iterations of &#39; + fun + &#39; took &#39; + time + &#39; milliseconds.&#39;);
    print(&#39;\t Average duration of a single run: &#39; + time/it + &#39;.\n&#39;);
  }
  for (const fun of abInputFunctions) {
    const time = timeFunction(fun, [a, b], it);
    print(it + &#39; iterations of &#39; + fun + &#39; took &#39; + time + &#39; milliseconds.&#39;);
    print(&#39;\t Average duration of a single run: &#39; + time/it + &#39;.\n&#39;);
  }
}

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);

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:

确定