网络编程 
首页 > 网络编程 > 浏览文章

JS实现的判断方法、变量是否存在功能示例

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
////www.jb51.net/article/67551.htm
//判断变量i是否存在 typeof(i)=="undefined"
<script>
 /*---------------------------判断函数是否存在-------------------------------*/
 function isExitsFunction(funcName) {
  try {
   if (typeof(eval(funcName)) == "function") {
    return true;
    // funcName();
   }
  } catch (e) {
   console.log(eval(funcName) + "+++++++++++++++++我异常了!!!!!!!!");
  }
  return false;
 }
 /*--------------------------------判断是否存在指定变量 -----------------------------------------*/
 function isExitsParamsVariable(variableName) {
  try {
   console.log("variableName.length===" + variableName.length);
   if (variableName.length == 0) {
    console.log(variableName + "===value has no params");//"":length为0
    return false;
   } else {
    console.log(variableName + "======value has params");//0:length为undefined
    return true;
   }
  } catch (e) {
   console.log(variableName + "----我异常了!!!!!!!!");//null,undefined,未赋值的a
  }
  return false;//null,undefined,未赋值的a
 }
 /*---------------------------------判断是否undefined--------------------------------*/
 function isExitsVariable(variableName) {
  console.log("typeof variableName====" + typeof(variableName));
  try {
   if (typeof(variableName) == "undefined") {
    console.log(variableName + "===value is undefined");//undefined,未赋值的a
    return false;
   } else {
    console.log(variableName + "=======value is true");//null,0,""
    return true;
   }
  } catch (e) {
   console.log(variableName + "-------我异常了........");
  }
  return false;
 }
 /*-------------------------------------------------测试数据---------------------------------------------*/
 var a;//声明未初始化,没有长度
 console.log("isExitsParamsVariable(a)" + isExitsParamsVariable(a));
 console.log("isExitsVariable(a)" + isExitsVariable(a));
 console.log("--------------------------------------------------")
 var b = undefined;//没有长度
 console.log("isExitsParamsVariable(b)===" + isExitsParamsVariable(b));
 console.log("isExitsVariable(b)===" + isExitsVariable(b));
 console.log("--------------------------------------------------")
 var c = null;//没有长度
 console.log("isExitsParamsVariable(c)===" + isExitsParamsVariable(c));
 console.log("isExitsVariable(c)===" + isExitsVariable(c));
 console.log("--------------------------------------------------")
 var d = 0;//长度undefined
 console.log("isExitsParamsVariable(d)===" + isExitsParamsVariable(d));
 console.log("isExitsVariable(d)===" + isExitsVariable(d));
 console.log("--------------------------------------------------")
 var e = "";//长度为0
 console.log("isExitsParamsVariable(e)====" + isExitsParamsVariable(e));
 console.log("isExitsVariable(e)===" + isExitsVariable(e));
 console.log("--------------------------------------------------")
 /*未定义声明 f 则log会报错:Uncaught ReferenceError: f is not defined ,不会执行两个判断方法*/
 console.log("isExitsParamsVariable(f)====" + isExitsParamsVariable(f));//f:undefined
 console.log("isExitsVariable(f)===" + isExitsVariable(f));
</script>
</body>
</html>

本文实例讲述了JS实现的判断方法、变量是否存在功能。分享给大家供大家参考,具体如下:

js 代码中经常会碰到 undefined 这种错误,下面本文分享一下为什么会发生这种错误以及如何处理这种错误,js 中如果通过 var 声明了一个变量但是没有初始化该变量的时候,此时该变量的值便为 undefined ,此时判断变量是否定义可使用 typeof 。下面举例说明一下

if(!result){ 
 alert("发生错误"); 
} 

以上这段代码直接运行会发生异常,因为变量 result 没有申明就被使用了,下面几种写法都是正确的。

(1) 
 
if("undefined" == typeof result){ 
 alert("发生错误"); 
} 
(2) 
 
var result; 
if(undefined == result){ 
 alert("发生错误"); 
} 
(3) 
 
if("undefined" == typeof result){ 
 alert("发生错误"); 
} 

补充

例如:

if(!myVar01)alert("发生错误");

// 该代码直接发生异常,因为变量myVar01没有申明 if("undefined" == typeof myVar01)alert("发生错误");

// 这样写才不至于发生异常

而: var myVar01; if(undefined == myVar01)alert("发生错误");

// 该代码会正确运行 if("undefined" == typeof myVar01)alert("发生错误");
// 该代码同样会正确运行

结论:我们采用下面的方式来保证万无一失 if("undefined" == typeof myVar01)alert("发生错误");

// 该代码同样会正确运行

当然判断数据的有效性远远不只这些,还有对null的判断,数字是否大道越界.

实例

<script>
//最常用
if("undefined" == typeof('a')){
//未定义
}else{
//定义
}

if("undefined" == typeof a){
//未定义
}else{
//定义
}

if(typeof a != "undefined"){
//true 定义
}else{
 //false 未定义
}
</script>

实际应用:

downlm有的页面我们不定义,但有的页面定义了,就可以需要这样的判断方法,没有定义的就不执行。

if("undefined" != typeof downlm){ 
if(downlm=="soft"){ 
document.write('成功'); 
} 
}

经测试完美。

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript字符与字符串操作技巧总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript数学运算用法总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript错误与调试技巧总结》

希望本文所述对大家JavaScript程序设计有所帮助。

上一篇:vue 标签属性数据绑定和拼接的实现方法
下一篇:vue 路由嵌套高亮问题的解决方法
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网