【发布时间】:2011-04-18 09:10:02
【问题描述】:
我对 JavaScript 很陌生,但这个话题似乎只吸引了很少的论坛关注。给定一些简单的函数:
function do_something(){...};
function do_somemore(){...};
function do_something_else(){...};
我希望能够将这些显式分配给(此处为二维)数组中的单元格。
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](); 调用前者我已经尝试过,但后者逃脱了我:-)
-
如果没有一个答案能满足您的问题,请考虑选择一个答案或添加更具体的信息。
标签: javascript arrays function