JS實(shí)現(xiàn)的判斷方法、變量是否存在功能示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
////www.dhdzp.com/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)有申明就被使用了,下面幾種寫(xiě)法都是正確的。
(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ò)誤");
// 這樣寫(xiě)才不至于發(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)容感興趣的讀者可查看本站專(zhuān)題:《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ì)有所幫助。
相關(guān)文章
Bootstrap入門(mén)書(shū)籍之(五)導(dǎo)航條、分頁(yè)導(dǎo)航
這篇文章主要介紹了Bootstrap入門(mén)書(shū)籍之(五)導(dǎo)航條、分頁(yè)導(dǎo)航的相關(guān)資料,需要的朋友可以參考下2016-02-02
BootStrap select2 動(dòng)態(tài)改變值的方法
這篇文章主要介紹了BootStrap select2 動(dòng)態(tài)改變值的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
利用JavaScript實(shí)現(xiàn)仿QQ個(gè)人資料卡效果
這篇文章主要為大家詳細(xì)介紹了如何利用HTML+CSS+JavaScript實(shí)現(xiàn)仿QQ個(gè)人資料卡效果,文中的示例代碼講解詳細(xì),感興趣的可以動(dòng)手嘗試一下2022-08-08

