js简单判断函数是否存在的方法,涉及javascript函数运行与类型判定相关操作技巧
第一段代码:
<!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中函数是否存在实例代码的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号