How can I implement an array of functions in JavaScript?

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

In Java, I would typically implement an array of 20 x 20 objects where each object would implement two functions. But, is this possible in JavaScript? If not, how should I proceed to get something similar? If I need two matrix to simulate the pair, this is OK too.

Upvotes: 4

Views: 1227

Answers (6)

Raynos
Raynos

Reputation: 169383

Consider using a function factory instead:

function makeFunction(one, two) {
  return function () {
    ...
  }
}

makeFunction(1,2);
// Not functionMatrix[1][2]

Upvotes: 1

Hogan
Hogan

Reputation: 70523

Here is a two-dimensional array of pairs of functions:

Almost all of them are anonymous functions except for one to show you how that would work.

You can use this like this: 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; }
      }
    ]
  ];

Upvotes: 0

jAndy
jAndy

Reputation: 235982

You cannot really have a matrix array structure in ECMAscript, but you can create Arrays of Arrays of course:

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 execute function o2 in the above example.

Upvotes: 3

Matt Ball
Matt Ball

Reputation: 359786

Since functions are objects in JavaScript, you can define an array of them quite easily.

function foo () { ... }
function bar () { ... }
function baz () { ... }

var fns = [foo, bar, baz];

The signature does not matter at all.


From there you can start dynamically generating functions in a loop, rather than explicitly declaring each one:

function generator(n)
{
    return function ()
    {
        return n*n;
    };
}

var squareFuncs = [];

for (var i=0; i<10; i++)
{
    squareFuncs.push(generator(i));
}

Then you can build up arrays of arrays of functions (just 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 (really, an array of arrays) of functions which compute the sum of the coordinates of that function in the matrix. That probably sounds more complicated than it really is, so here's an example:

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

Related:

Upvotes: 8

AaronHolland
AaronHolland

Reputation: 1675

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

Upvotes: 1

BNL
BNL

Reputation: 7133

You can create an object with two functions and put that into your array.

Upvotes: 2

Related Questions