如何在JavaScript中实现一系列函数? [英] How can I implement an array of functions in JavaScript?

查看:63
本文介绍了如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆