[英]How to execute a JavaScript function having its name as a string in Edge
function test(arg) { console.log("hello! " + arg); return true; } window["test"]("I AM");
In this code snippet I execute a function by its name as a string. 在这段代码中,我通过名称作为字符串执行函数。 It works in most browsers except Microsoft Edge.
它适用于除Microsoft Edge之外的大多数浏览器。 How can I execute a function in Edge if I only know its name as a string?
如果我只知道它的名字是字符串,我如何在Edge中执行一个函数? I don't want to use eval().
我不想使用eval()。
If you define your function straight to the window object then you can call it just with the string. 如果直接将函数定义到window对象,则可以使用字符串调用它。
window.test=(args)=>{console.log("hello! " + arg)}
window["test"]("My first stack overflow answer")
will return "hello! My first stack overflow answer" 将返回“你好!我的第一个堆栈溢出答案”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.