How to implement an array of functions in Javascript?

Still new to Javascript. I need to encode a 20 x 20 matrix pair of functions. All my functions take a number and return a number (i.e. the same signature). For example, Myfunctions [1] [2] should return to me a couple of functions that I could call in the code.

In Java, I would usually implement an array of 20 x 20 objects, where each object would implement two functions. But is this possible in Javascript? If not, how do I get started with something like this? If I need two matrices to simulate a pair, that's fine too.

Thanks.

+4
source share
6 answers

In ECMAscript, you really cannot have the matrix structure of an array, but you can, of course, create arrays of arrays:

function o1() {}; function o2() {}; function o3() {}; function o4() {}; function o5() {}; function o6() {}; var Myfunctions = [ [ o1, o2 ], [ o3, o4 ], [ o5, o6 ] ]; 

Now

 Myfunctions[0][1](); 

would perform the o2 function in the above example.

+3
source

Since functions are objects in JavaScript, you can easily define their array.

 function foo () { ... } function bar () { ... } function baz () { ... } var fns = [foo, bar, baz]; 

The signature does not matter.


From there, you can run dynamically generating functions in a loop, rather than explicitly declaring each of them:

 function generator(n) { return function () { return n*n; }; } var squareFuncs = []; for (var i=0; i<10; i++) { squareFuncs.push(generator(i)); } 

Then you can create arrays of arrays of functions (like any other object, remember):

 function anotherGenerator(a, b) { return function () { return a+b; }; } var sumFuncs = [], temp, i, j; for (i=0; i<20; i++) { temp = []; for (j=0; j<20; j++) { temp.push(anotherGenerator(i, j)); } sumFuncs.push(temp); } 

Now sumFuncs is a two-dimensional array (actually an array of arrays) of functions that calculate the sum of the coordinates of this function in the matrix. This probably sounds more complicated than it really is, here is an example:

 var foo = sumFuncs[7][2], sum = foo(); console.log(sum); // prints 9 

on this topic:

+8
source

You can create an object with two functions and put it in your array.

+2
source

Have you considered factory function instead

 function makeFunction(one, two) { return function () { ... } } makeFunction(1,2); // not functionMatrix[1][2] 
+1
source
 Myfunctions[1][2] = { first: function(val) { return val + 1; } second: function(val) { return val - 1; } }; firstVal = Myfunctions[1][2].first(100); secondVal = Myfunctions[1][2].second(100); 
+1
source

Here is the 2nd array of function pairs:

Almost all of them are anonymous functions, except for one, to show you how this will work.

You can use this as follows: var myresult = myFuncs[2][3].func1(45);

 function extern2ArrayFunc (a) { return a+; } var myFuncs = [ [ { "func1" : function (a) { return a+1; }, "func2" : function (b) { return b-1; } }, { "func1" : exter2ArrayFunc }, "func2" : function (b) { return b-1; } }, { "func1" : function (a) { return a+1; }, "func2" : function (b) { return b-1; } } ], [ { "func1" : function (a) { return a+1; }, "func2" : function (b) { return b-1; } }, { "func1" : function (a) { return a+1; }, "func2" : function (b) { return b-1; } }, { "func1" : function (a) { return a+1; }, "func2" : function (b) { return b-1; } } ], [ { "func1" : function (a) { return a+1; }, "func2" : function (b) { return b-1; } }, { "func1" : function (a) { return a+1; }, "func2" : function (b) { return b-1; } }, { "func1" : function (a) { return a+1; }, "func2" : function (b) { return b-1; } } ] ]; 
0
source

Source: https://habr.com/ru/post/1383360/


All Articles