Harry
Harry

Reputation: 54949

javascript run a bunch of functions

How do i execute a bunch of functions without knowing their names?

var theseFn = function () {
  func1 : function () {},
  func2 : function () {}
}

I want to run everything in theseFn. How do I do this? Thanks.

Upvotes: 1

Views: 166

Answers (4)

icktoofay
icktoofay

Reputation: 129011

You could use a for-in loop (with a hasOwnProperty check, of course) with bracket notation for object property access:

for(var functionName in theseFn) {
    if(theseFn.hasOwnProperty(functionName)&&typeof theseFn[functionName]=="function") {
        theseFn[functionName]();
    }
}

Upvotes: 5

Stoive
Stoive

Reputation: 11322

This will execute all functions on an object, assuming no arguments:

for (var i in theseFn) if (typeof(theseFn[i]) === "function") theseFn[i]();

Upvotes: 6

Hogan
Hogan

Reputation: 70523

I think you mean

var theseFn = {
   func1 : function () {},
   func2 : function () {}
}

then you can say

 theseFn.func1();

Upvotes: 0

Jon
Jon

Reputation: 437366

Iterate over the properties of theseFn, invoking every one in turn:

for (func in theseFn)
{
    theseFn[func]();
}

Upvotes: 0

Related Questions