I have the following code.
How can I execute the the clearSearch method as part of the methods Array?
//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);
asked Feb 19, 2014 at 16:49
hermann
6,31511 gold badges48 silver badges66 bronze badges
2 Answers 2
If you have the function in your scope:
function clearSearch() { ... }
Then you should reference it by name not by the string and push that:
task.methods.push(clearSearch);
To execute it you can do now:
task.methods[i]();
Where i is the index of the desired function in the array.
answered Feb 19, 2014 at 16:51
pid
11.6k6 gold badges37 silver badges67 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
hermann
OK, however it is not in the scope, it's under a controller in a different file.
You can put the clearSearch function in an object like this:
var obj = {
clearSearch: function () {
console.log('Search cleared');
}
};
Then you can call it like this:
obj['clearSearch']();
See this MDN Article on bracket notation for more information.
answered Feb 19, 2014 at 17:18
Sumner Evans
9,2055 gold badges32 silver badges48 bronze badges
Comments
lang-js
clearSearchmethod? Where is it defined?