How to call a function from ...spread?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
7 views
In the case where it is necessary to pass multiple callback functions, how to call them out ...spread? And is it right to use ... for this?
by | 7 views

3 Answers

0 like 0 dislike
The question is absolutely incomprehensible, but I'll try play the guessing game:
const f = (...callbacks) => { // spread collects arguments into an array, // so you can work with them as an array callbacks.forEach((callback) => callback()); } f( () => console.log(1), () => console.log(2), () => console.log(42), );
by
0 like 0 dislike
If you pass multiple variables, and collect and spread, you get an array of variables, so here, only the elements of the array will function.
by
0 like 0 dislike
Why not do an array of functions?

function f (callbacks) { for (let i = 0; i < callbacks.length; i++) { callbacks[i](); } } f ( [ _ => console.log(0), _ => console.log(1), _ => console.log(10), ] );
by

Related questions

0 like 0 dislike
3 answers
asked Aug 11, 2019 by zwezew
0 like 0 dislike
2 answers
asked Jun 9, 2019 by tokmaganbet
0 like 0 dislike
2 answers
asked May 22, 2019 by campus1
110,608 questions
257,186 answers
0 comments
27,923 users