我有以下代码。
如何将clearSearch 方法作为方法数组的一部分执行?
//Assume this is in a different file, inside a controller.
//I have access to the controller through a global variable.
function clearSearch() {
console.log('Search cleared');
}
.
function Task() {
this.controllers = [];
this.stores = [];
this.methods = [];
}
var taskList = [];
var task = new Task();
task.controllers.push('Search');
task.stores.push('search.Case', 'search.Result');
task.methods.push('clearSearch');
taskList.push(task);
最佳答案
如果您的范围内有该函数:
function clearSearch() { ... }
然后你应该通过名称而不是字符串引用它并推送它:
task.methods.push(clearSearch);
要执行它,您现在可以执行以下操作:
task.methods[i]();
其中 i
是数组中所需函数的索引。
关于javascript - 作为 JS 方法执行数组字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21886946/