Reputation: 924
I have an array which I'm adding objects to dynamically like so
var _plugins = [];
this.registerPlugin = function(plugin){
_plugins.push(plugin);
plugin.onInit()
},
This is all within a class and I am trying to use a method like this which should run the method passed in to meth
this.runPluginMethod = function(meth, call_obj){
for (x in _plugins){
x[meth](call_obj)
}
}
The Objects I am adding to the _plugins array are created like this
var ourPlugin = Object.create(babblevoicePlugin);
Object.defineProperty(ourPlugin, 'onInit', {value : function()
{
console.log('this is from reflex oninit')
}});
When I try running mianClass.runPluginMethod('onInit', 'a')
It does nothing, doesn't run console.log like it should to my mind.
Can anyone help? am I doing something wrong? is this possible?
Upvotes: 0
Views: 57
Reputation: 9779
I think the problem is here:
this.runPluginMethod = function(meth, call_obj){
for (x in _plugins){
x[meth](call_obj)
}
}
You're trying to access a property of a key instead of the object you're looking for. Changing it to the following should work.
this.runPluginMethod = function(meth, call_obj){
for (x in _plugins){
_plugins[x][meth](call_obj)
}
}
EDIT
As another example, check the output of the following in a js console:
x = ['a','b','c'];
for (i in x){ console.log(i, x[i]) };
Upvotes: 1