Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and call javascript functions in global scope

Tags:

javascript

I have registered some javascript functions in the global scope:

function Test1() {}
function Test2() {}

Now I want to run all javascript functions whose name starts with 'Test', how to do it?

I want to prevent saving each functions into a variable because it does not scale. Nor to push them to a queue and execute them later, since people need to remember adding their functions to the queue when they write the test and I don't want that.

like image 205
yijiem Avatar asked Jan 28 '23 09:01

yijiem


1 Answers

var globalKeys = Object.keys(window);

for(var i = 0; i < globalKeys.length; i++){
  var globalKey = globalKeys[i];
  if(globalKey.includes("Test") && typeof window[globalKey] == "function"){
    window[globalKey]();
  }
}
like image 69
Stakvino Avatar answered Jan 30 '23 00:01

Stakvino