溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-09-25 11:42:19 來(lái)源:腳本之家 閱讀:152 作者:Mars-xq 欄目:web開(kāi)發(fā)
<!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>
 /*---------------------------判斷函數(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;
 }
 /*-------------------------------------------------測(cè)試數(shù)據(jù)---------------------------------------------*/
 var a;//聲明未初始化,沒(méi)有長(zhǎng)度
 console.log("isExitsParamsVariable(a)" + isExitsParamsVariable(a));
 console.log("isExitsVariable(a)" + isExitsVariable(a));
 console.log("--------------------------------------------------")
 var b = undefined;//沒(méi)有長(zhǎng)度
 console.log("isExitsParamsVariable(b)===" + isExitsParamsVariable(b));
 console.log("isExitsVariable(b)===" + isExitsVariable(b));
 console.log("--------------------------------------------------")
 var c = null;//沒(méi)有長(zhǎng)度
 console.log("isExitsParamsVariable(c)===" + isExitsParamsVariable(c));
 console.log("isExitsVariable(c)===" + isExitsVariable(c));
 console.log("--------------------------------------------------")
 var d = 0;//長(zhǎng)度undefined
 console.log("isExitsParamsVariable(d)===" + isExitsParamsVariable(d));
 console.log("isExitsVariable(d)===" + isExitsVariable(d));
 console.log("--------------------------------------------------")
 var e = "";//長(zhǎng)度為0
 console.log("isExitsParamsVariable(e)====" + isExitsParamsVariable(e));
 console.log("isExitsVariable(e)===" + isExitsVariable(e));
 console.log("--------------------------------------------------")
 /*未定義聲明 f 則log會(huì)報(bào)錯(cuò):Uncaught ReferenceError: f is not defined ,不會(huì)執(zhí)行兩個(gè)判斷方法*/
 console.log("isExitsParamsVariable(f)====" + isExitsParamsVariable(f));//f:undefined
 console.log("isExitsVariable(f)===" + isExitsVariable(f));
</script>
</body>
</html>

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

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

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

以上這段代碼直接運(yùn)行會(huì)發(fā)生異常,因?yàn)樽兞?result 沒(méi)有申明就被使用了,下面幾種寫法都是正確的。

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

補(bǔ)充

例如:

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

// 該代碼直接發(fā)生異常,因?yàn)樽兞縨yVar01沒(méi)有申明 if("undefined" == typeof myVar01)alert("發(fā)生錯(cuò)誤");

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

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

// 該代碼會(huì)正確運(yùn)行 if("undefined" == typeof myVar01)alert("發(fā)生錯(cuò)誤");
// 該代碼同樣會(huì)正確運(yùn)行

結(jié)論:我們采用下面的方式來(lái)保證萬(wàn)無(wú)一失 if("undefined" == typeof myVar01)alert("發(fā)生錯(cuò)誤");

// 該代碼同樣會(huì)正確運(yùn)行

當(dāng)然判斷數(shù)據(jù)的有效性遠(yuǎn)遠(yuǎn)不只這些,還有對(duì)null的判斷,數(shù)字是否大道越界.

實(shí)例

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

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

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

實(shí)際應(yīng)用:

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

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

經(jīng)測(cè)試完美。

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript字符與字符串操作技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI