我对JavaScript很陌生,但这个话题似乎只吸引了很少的论坛关注。给定一些简单的函数:使用JavaScript从数组中激发命名函数?
function do_something(){...};
function do_somemore(){...};
function do_something_else(){...};
我期待能够明确地将这些分配给(这里是2D)数组中的单元格。
myMatrix[5][3] = do_something();
myMatrix[5][4] = do_somemore();
myMatrix[5][5] = do_something_else();
我想用这种方法的原因是:
- 易于理解和维护。
- 消除了阵列中潜在的多余匿名函数分配。
任何给定的功能可以被分配给多个阵列单元,例如:
myMatrix[2][6] = do_somemore(); myMatrix[5][4] = do_somemore(); myMatrix[6][3] = do_somemore();
不幸的是,调用诸如以下(基于各种论坛实例中,再加上一点点“吸,看看“)都失败了。
x = myMatrix[5][4]do_somemore(); -> "missing ; before statement"
x = (myMatrix[5][4])do_somemore(); -> "missing ; before statement"
x = (myMatrix[5][4]do_somemore)(); -> "missing) in parenthetical"
x = (myMatrix[5][4])(do_somemore()); -> "is not a function"
x = (myMatrix[5][4])()do_somemore(); -> "missing ; before statement"
x = myMatrix[5][4]()do_somemore(); -> "missing ; before statement"
x = myMatrix[5][4](); -> "is not a function"
x = (myMatrix[5][4])(); -> "is not a function"
因为我没有JavaScript的内部知识,我会很高兴的建议,如何让函数调用射击。
非常感谢所有贡献见解的人。该代码在几分钟内正确工作,分配如下: myMatrix [5] [4] = do_somemore; ..然后使用myMatrix调用[left] [right](); 前者我已经尝试过,但后者已经逃脱了我:-) – Thug 2011-04-18 15:48:06
请考虑选择一个答案或添加更多具体信息,如果没有答案满足您的问题。 – 2013-07-10 11:04:24