【发布时间】:2014-07-13 21:27:27
【问题描述】:
在下面的代码中,我尝试创建几个处理函数,它们必须调用存储在函数数组('buttonHandlers')中的不同函数。这个数组是外部作用域的一部分:
buttonJson = {};
for (i = 0; i < buttonNames.length; i++) {
customHandler = buttonHandlers[i];
buttonJson[buttonNames[i]] = function() {
customHandler.apply();
$('#msg-dialog-confirm').dialog("close");
$('body').remove('#msg-dialog-confirm');
...
};
}
上面的代码生成了调用函数数组的最后一个数组元素('buttonHandlers')的处理函数。我希望每个处理函数只调用相关数组索引指定的关联函数。我怎样才能做到这一点?
【问题讨论】:
-
试试
buttonJson[buttonNames[i]] = function(customHandler) { -
它不起作用。可能,这个提示太短了。无论如何,我需要将函数传递给这个参数。
标签: javascript arrays scope closures