检查函数是否存在,其名称是否在字符串中? [英] check if a function exists with its name in a string?

查看:94
本文介绍了检查函数是否存在,其名称是否在字符串中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Javascript 中动态创建函数。有时我需要检查某个函数是否实际上已经创建。

I create functions in Javascript dynamically. Sometimes I need to check if a certain function is actually already created.

我将函数的名称作为字符串。如何根据字符串中的给定值检查函数是否存在?

I have the name of the function as a string. How can I check whether a function exists based on a given value in a string?

推荐答案

您可以检查它是否在全局范围使用;

You can check whether it's defined in the global scope using;

if (typeof window[strOfFunction] === "function") {
    // celebrate
    //window[strOfFunction](); //To call the function dynamically!
}

这篇关于检查函数是否存在,其名称是否在字符串中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆