如何在JavaScript中实现一系列函数? [英] How can I implement an array of functions in JavaScript?
问题描述
我还是JavaScript的新手.我需要编写20 x 20矩阵对函数的代码.我所有的函数都接受一个数字并返回一个数字(即,相同的签名).例如,Myfunctions [1] [2]应该给我返回了我可以在代码中调用的一对函数.
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.
在Java中,我通常将实现一个20 x 20对象的数组,其中每个对象将实现两个功能.但是,这在JavaScript中可行吗?如果没有,我应该如何继续获得类似的东西?如果我需要两个矩阵来模拟一对,也可以.
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.
推荐答案
您实际上不能在ECMAscript中拥有矩阵数组结构,但是您当然可以创建数组数组:
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 ]
];
现在
Myfunctions[0][1]();
在上面的示例中将执行功能o2
.
would execute function o2
in the above example.
这篇关于如何在JavaScript中实现一系列函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!