欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實現(xiàn)的判斷方法、變量是否存在功能示例

 更新時間:2020年03月28日 21:30:00   作者:Mars-xq  
這篇文章主要介紹了JS實現(xiàn)的判斷方法、變量是否存在功能,涉及javascript針對變量、方法判斷與異常處理操作技巧,需要的朋友可以參考下
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
////www.dbjr.com.cn/article/67551.htm
//判斷變量i是否存在 typeof(i)=="undefined"
<script>
 /*---------------------------判斷函數(shù)是否存在-------------------------------*/
 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;
 }
 /*-------------------------------------------------測試數(shù)據---------------------------------------------*/
 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 ,不會執(zhí)行兩個判斷方法*/
 console.log("isExitsParamsVariable(f)====" + isExitsParamsVariable(f));//f:undefined
 console.log("isExitsVariable(f)===" + isExitsVariable(f));
</script>
</body>
</html>

本文實例講述了JS實現(xiàn)的判斷方法、變量是否存在功能。分享給大家供大家參考,具體如下:

js 代碼中經常會碰到 undefined 這種錯誤,下面本文分享一下為什么會發(fā)生這種錯誤以及如何處理這種錯誤,js 中如果通過 var 聲明了一個變量但是沒有初始化該變量的時候,此時該變量的值便為 undefined ,此時判斷變量是否定義可使用 typeof 。下面舉例說明一下

if(!result){ 
 alert("發(fā)生錯誤"); 
} 

以上這段代碼直接運行會發(fā)生異常,因為變量 result 沒有申明就被使用了,下面幾種寫法都是正確的。

(1) 
 
if("undefined" == typeof result){ 
 alert("發(fā)生錯誤"); 
} 
(2) 
 
var result; 
if(undefined == result){ 
 alert("發(fā)生錯誤"); 
} 
(3) 
 
if("undefined" == typeof result){ 
 alert("發(fā)生錯誤"); 
} 

補充

例如:

if(!myVar01)alert("發(fā)生錯誤");

// 該代碼直接發(fā)生異常,因為變量myVar01沒有申明 if("undefined" == typeof myVar01)alert("發(fā)生錯誤");

// 這樣寫才不至于發(fā)生異常

而: var myVar01; if(undefined == myVar01)alert("發(fā)生錯誤");

// 該代碼會正確運行 if("undefined" == typeof myVar01)alert("發(fā)生錯誤");
// 該代碼同樣會正確運行

結論:我們采用下面的方式來保證萬無一失 if("undefined" == typeof myVar01)alert("發(fā)生錯誤");

// 該代碼同樣會正確運行

當然判斷數(shù)據的有效性遠遠不只這些,還有對null的判斷,數(shù)字是否大道越界.

實例

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

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

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

實際應用:

downlm有的頁面我們不定義,但有的頁面定義了,就可以需要這樣的判斷方法,沒有定義的就不執(zhí)行。

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

經測試完美。

更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript字符與字符串操作技巧總結》、《JavaScript遍歷算法與技巧總結》、《JavaScript查找算法技巧總結》、《JavaScript數(shù)學運算用法總結》、《JavaScript數(shù)據結構與算法技巧總結》及《JavaScript錯誤與調試技巧總結

希望本文所述對大家JavaScript程序設計有所幫助。

相關文章

最新評論