0

I have an object that contains a lot of arrays inside. This array content function-paramenters.

for example: object = {"elem" : [fn1, fn2], "other-elem" : [fn3, fn4, fn5], ... }

I want to make a method who receive a name and when this name fixes with the object element key, it have to execute all the functions that are in this array.

How can I exec this functions?

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
Iban Arriola
  • 2,526
  • 9
  • 41
  • 88

3 Answers3

2

use this to get your functions

 object["elem"]

in jQuery you could use

$(object["elem"]).each(function(index,value){
   value();
});

in native JavaScript you could use

for(var i = 0; i < object["elem"].length; i++)
{
   object["elem"][i]();
}
Ruben-J
  • 2,663
  • 15
  • 33
1

You just call it by using () :

var fnarray = object[name];
var i;
for (i = 0; i<fnarray.length; i++) {
  fnarray[i](); // execute the function
}
drquicksilver
  • 1,627
  • 9
  • 12
1

Your Exec function should be like this

function Exec(ElementName)
{
    if (object[ElementName])
    {
        for (var i = 0; i < object[ElementName].length; i++)
            object[ElementName][i]();
    }
}
Exec('elem');
999k
  • 6,257
  • 2
  • 29
  • 32