<!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程序设计有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。