开发者

javascript run a bunch of functions

开发者 https://www.devze.com 2023-02-18 21:13 出处:网络
How do i execute a bunch of functions without knowing their names? var theseFn = function () { func1 : function () {},

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号