admin管理员组文章数量:1386660
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.
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.
Share Improve this question asked Sep 24, 2018 at 22:43 yijiemyijiem 3693 silver badges18 bronze badges 7- are the functions named "Test" + number or "Test" + anything else ? – Stakvino Commented Sep 24, 2018 at 22:49
-
1
Iterate over all properties of
window
and check whether it starts withTest
? 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? – Felix Kling 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. – Matt Way 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 – Felix Kling Commented Sep 24, 2018 at 22:53
- Also related: “Variable” variables in Javascript? – Felix Kling Commented Sep 24, 2018 at 22:58
3 Answers
Reset to default 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]();
}
}
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 = () => {/*...*/}
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.
本文标签: Find and call javascript functions in global scopeStack Overflow
版权声明:本文标题:Find and call javascript functions in global scope - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744569623a2613258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论