编码中可能要判断一个函数是否存在,下面分享一段能够实现此功能的代码。
/ A. f$ w& w n- w0 T3 u! M# U$ M l# ~代码示例如下:
( k6 W) V) b+ a+ G& e7 Xfunction isExitsFunction(funcName){
try {
if (typeof(eval(funcName)) == "function"){
return true;
}
}
catch(e) {}
return false;
}
var func=function(){}
console.log(isExitsFunction(func)); 如果函数存在,那么isExitsFunction函数的返回值为true,否则返回false。2 _! w0 X2 M' d" N/ ?
/ ^3 V. z4 s/ k
|