通过在变量[duplicate]中存储函数名来调用js中的函数

时间:2021-05-30 16:47:21

This question already has an answer here:

这个问题在这里已有答案:

I have one function named root and at next part I am storing function name in variable (call) . I want to call function by using variable name For Example:-

我有一个名为root的函数,在下一部分我将函数名存储在变量(call)中。我想通过使用变量名来调用函数例如: -

(note:- not using root() using variable call's value )

(注意: - 不使用变量调用值的root())

 call = "root";

 root = function() {
    name = "This is My root function";
    console.log(name)
 } 

Can we call root function by value of variable (call) Is it possible.

我们可以通过变量值调用root函数(call)是否可能。

2 个解决方案

#1


2  

Assuming call is a global browser variable, you can do:

假设call是一个全局浏览器变量,你可以这样做:

window[call]();

#2


2  

eval() is probably what you need, but use with caution.

eval()可能就是你所需要的,但要谨慎使用。

#1


2  

Assuming call is a global browser variable, you can do:

假设call是一个全局浏览器变量,你可以这样做:

window[call]();

#2


2  

eval() is probably what you need, but use with caution.

eval()可能就是你所需要的,但要谨慎使用。