[英]Define dynamic function in javascript
I am having SetUpTest()
method, function name will be generated on-load
of script by fetching window object.我有SetUpTest()
方法,function 名称将通过获取 window object 在脚本on-load
时生成。 testShieldVal.js_name
is variable, testShieldVal.js_name
是变量,
testShieldVal = window.testObject || {};
SetUpTest = testShieldVal.js_name+"SetUp";
function SetUpTest() {
new atoShieldVal.js_name.OnloadCallback(grecaptcha);
}
How can I define this function with dynamic name efficiently?如何有效地使用动态名称定义此 function?
If I understand your question correctly, you are looking to define a function with a dynamic name.如果我正确理解您的问题,您正在寻找使用动态名称定义 function 。
In that case you should add the method to the window
object using window[functionName] = function() {... }
在这种情况下,您应该使用window[functionName] = function() {... }
将方法添加到window
object
This results in:这导致:
testShieldVal = window.testObject || {};
SetUpTest = testShieldVal.js_name+"SetUp";
window[SetUpTest] = function {
new atoShieldVal.js_name.OnloadCallback(grecaptcha);
}
When window.testObject.js_name
is 'Demo' then Your function can then be called via DemoSetUp()
.当window.testObject.js_name
为“Demo”时,您的 function 可以通过DemoSetUp()
调用。 This will automatically look in the window
object for a method with that name.这将自动在window
object 中查找具有该名称的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.