<!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; } /*-------------------------------------------------測試數(shù)據(jù)---------------------------------------------*/ 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)的判斷方法、變量是否存在功能。分享給大家供大家參考,具體如下:
為新賓等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及新賓網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、新賓網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
js 代碼中經(jīng)常會碰到 undefined 這種錯誤,下面本文分享一下為什么會發(fā)生這種錯誤以及如何處理這種錯誤,js 中如果通過 var 聲明了一個變量但是沒有初始化該變量的時候,此時該變量的值便為 undefined ,此時判斷變量是否定義可使用 typeof 。下面舉例說明一下
if(!result){ alert("發(fā)生錯誤"); }
以上這段代碼直接運(yùn)行會發(fā)生異常,因為變量 result 沒有申明就被使用了,下面幾種寫法都是正確的。
(1) if("undefined" == typeof result){ alert("發(fā)生錯誤"); } (2) var result; if(undefined == result){ alert("發(fā)生錯誤"); } (3) if("undefined" == typeof result){ alert("發(fā)生錯誤"); }
補(bǔ)充
例如:
if(!myVar01)alert("發(fā)生錯誤");
// 該代碼直接發(fā)生異常,因為變量myVar01沒有申明 if("undefined" == typeof myVar01)alert("發(fā)生錯誤");
// 這樣寫才不至于發(fā)生異常
而: var myVar01; if(undefined == myVar01)alert("發(fā)生錯誤");
// 該代碼會正確運(yùn)行 if("undefined" == typeof myVar01)alert("發(fā)生錯誤");
// 該代碼同樣會正確運(yùn)行
結(jié)論:我們采用下面的方式來保證萬無一失 if("undefined" == typeof myVar01)alert("發(fā)生錯誤");
// 該代碼同樣會正確運(yùn)行
當(dāng)然判斷數(shù)據(jù)的有效性遠(yuǎn)遠(yuǎn)不只這些,還有對null的判斷,數(shù)字是否大道越界.
實例
<script> //最常用 if("undefined" == typeof('a')){ //未定義 }else{ //定義 } if("undefined" == typeof a){ //未定義 }else{ //定義 } if(typeof a != "undefined"){ //true 定義 }else{ //false 未定義 } </script>
實際應(yīng)用:
downlm有的頁面我們不定義,但有的頁面定義了,就可以需要這樣的判斷方法,沒有定義的就不執(zhí)行。
if("undefined" != typeof downlm){ if(downlm=="soft"){ document.write('成功'); } }
經(jīng)測試完美。
更多關(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錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
網(wǎng)頁題目:JS實現(xiàn)的判斷方法、變量是否存在功能示例
分享網(wǎng)址:http://aaarwkj.com/article44/iijhee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)頁設(shè)計公司、網(wǎng)站改版、網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)