时间:2021-07-01 10:21:17 帮助过:6人阅读
第一段代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript" language="javascript" >
function showFace(unipcn,value){}
var isFunction =false;
try{
//这里的代码需要用try一下,因为当showFace为定义时会抛出异常
isFunction = typeof(eval('showFace'))=="function";
}catch(e){}
if(isFunction) {
alert('showFace is a Function!');
}else{
alert('showFace is not a Function!');
}
</script>
</head>
<body>
</body>
</html>第二段代码:
window.onload=function(){
try{
if(test&&typeof(test)=="function"){
test();
}else{
alert("不存在的函数");
}
}catch(e){
}
}
function test(){
alert("函数执行……");
}如果不存在,会抛出异常,所以要加try……catch。
以上就是两种方式判断javascript中函数是否存在实例代码的详细内容,更多请关注Gxl网其它相关文章!