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开发者_JAVA百科 theseFn. How do I do this? Thanks.
This will execute all functions on an object, assuming no arguments:
for (var i in theseFn) if (typeof(theseFn[i]) === "function") theseFn[i]();
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]();
}
}
Iterate over the properties of theseFn
, invoking every one in turn:
for (func in theseFn)
{
theseFn[func]();
}
I think you mean
var theseFn = {
func1 : function () {},
func2 : function () {}
}
then you can say
theseFn.func1();
精彩评论