4

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.

7
  • are the functions named "Test" + number or "Test" + anything else ? Commented Sep 24, 2018 at 22:49
  • 1
    Iterate over all properties of window and check whether it starts with Test? But since there are lots of things in global scope, maintaining your own list is probably better. Naming variables/functions with consecutive numbers is a string sign that you should be using an array instead. "I want to prevent saving each functions into a variable because it does not scale." Can you elaborate what you mean? Commented Sep 24, 2018 at 22:50
  • I want to prevent saving each functions into a variable because it does not scale. Can you elaborate on this, because it doesn't sound right. Commented Sep 24, 2018 at 22:53
  • If you don't provide more/other information then this is a duplicate of Calling multiple functions with names matching Regex Commented Sep 24, 2018 at 22:53
  • Also related: “Variable” variables in Javascript? Commented Sep 24, 2018 at 22:58

3 Answers 3

6

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]();
  }
}

Sign up to request clarification or add additional context in comments.

6 Comments

that's two loops, an if`(globalKey.includes ...) window[globalKey]() in first (and only) loop would be way better imho
Thanks Stakvino, this looks clean. I thought about searching all objects under global scope. I am very new to javascript and surprised to see there is no better way of doing it.
@Loïc true you can do it in one loop i did the modification. yijiem : yes unfortunately there is no straight froward way to do it in Javascript
I actually prefer the first version with 2 loops, it looks cleaner than this one. This one might be preferred if we have a performance issue and want to optimize to extreme in real production.
@yijiem Exactly
|
2
function Test() { console.log('test') }
Object.keys(window).filter(s => s.startsWith('Test')) // [ "Test" ]

As you can see, functions are defined on the global scope.

const isTest = s => typeof s === 'function' && s.startsWith('Test')
Object.keys(window).filter(isTest).map(t => t())

I don't know your use case entirely, but I suspect it would be better to provide your own object.

const tests = {}
tests.Test1 = () => {/*...*/}

3 Comments

I don't understand the last part. My use case is that I will have a piece of javascript code that executes all test functions (like the 1st and 2nd part of your answer) and others can add their own test cases as separate functions and not worry about the execution.
It seems to me that you are combining two practises which are both sub-optimal: 1. defining global variables, 2. relying on naming convention to call functions. It would be better to simply define all functions to be called as properties of some object, that way test-case authors could write their functions however they wanted, and know that they'd be called, because all functions that are children of tests will be called.
Good idea! This also does not put too much overhead on test author (they just need to remember to add their test case to the global test object).
0

You could get all elements of window and check them. Here is a basic starting point :

for(let objectName in window){ console.log(`${objectName} : ${typeof window[objectName]}`) }

Now objectName is actually a string, it could be anything, just check if it starts with Test, and if its type is a function.

1 Comment

You can (and should) use window[object] instead of eval(...). eval('window.'+object) will fail if object is "foo bar".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.