Javascript判断是否存在函数的方法_javascript技巧
来源:动视网
责编:小采
时间:2020-11-27 21:06:25
Javascript判断是否存在函数的方法_javascript技巧
Javascript判断是否存在函数的方法_javascript技巧: 代码如下: window.onload=function(){ try{ if(test&&typeof(test)==function){ test(); }else{ alert(不存在的函数); } }catch(e){ } } function test(){ alert(函数执行……); } 如果不存在,会抛出异常,所以要
导读Javascript判断是否存在函数的方法_javascript技巧: 代码如下: window.onload=function(){ try{ if(test&&typeof(test)==function){ test(); }else{ alert(不存在的函数); } }catch(e){ } } function test(){ alert(函数执行……); } 如果不存在,会抛出异常,所以要

代码如下:
window.onload=function(){
try{
if(test&&typeof(test)=="function"){
test();
}else{
alert("不存在的函数");
}
}catch(e){
}
}
function test(){
alert("函数执行……");
}
如果不存在,会抛出异常,所以要加try……catch。
Javascript判断是否存在函数的方法_javascript技巧
Javascript判断是否存在函数的方法_javascript技巧: 代码如下: window.onload=function(){ try{ if(test&&typeof(test)==function){ test(); }else{ alert(不存在的函数); } }catch(e){ } } function test(){ alert(函数执行……); } 如果不存在,会抛出异常,所以要