如何实现的功能在Javascript数组? [英] How to implement an array of functions in Javascript?

查看:113
本文介绍了如何实现的功能在Javascript数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然是一个新手的JavaScript。我需要code一20×20矩阵对功能。我所有的函数都在一个数字并返回一个数(即,相同的签名)。例如Myfunctions [1] [2]应该返回我一双功能,我可以在code调用。

Still a newbie 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×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

这篇关于如何实现的功能在Javascript数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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