Javascript在调用之前查找函数/类是否存在

puk*_*puk 2 javascript function exists

我知道如何检查是否存在全局上下文的属性.任何变化

if (typeof myFunction != 'undefined'){...}
Run Code Online (Sandbox Code Playgroud)

但如果我不知道该功能的名称怎么办?我认为全球我可以做到这一点

if (typeof this['myFunction'] != 'undefined'){...}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何在这样的功能中做到这一点

function load(functionName){
  if (typeof GLOBALCONTEX[functionName] != 'undefined'){
    GLOBALCONTEX[functionName](arg1 , arg2 , ...);
  }
}
Run Code Online (Sandbox Code Playgroud)

而且我不想使用try/catch,因为我听说它很慢.

Sha*_*haz 8

如果使用浏览器,请替换GLOBALCONTEXwindow.例:

function load(functionName){
  if (typeof window[functionName] != 'undefined'){
   window[functionName](arg1 , arg2 , ...);
  }
}
Run Code Online (Sandbox Code Playgroud)