国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

JS實現的判斷方法、變量是否存在功能示例

2024-05-06 16:44:27
字體:
來源:轉載
供稿:網友

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

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Title</title></head><body>//http://m.survivalescaperooms.com/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>

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


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 溆浦县| 清丰县| 集安市| 环江| 项城市| 布尔津县| 肃宁县| 怀安县| 阳原县| 芦溪县| 邵阳市| 巩义市| 措勤县| 乌拉特后旗| 易门县| 舟山市| 白山市| 乌鲁木齐县| 大石桥市| 柞水县| 肇东市| 永丰县| 万源市| 丘北县| 枣阳市| 奈曼旗| 祁连县| 福建省| 和龙市| 东台市| 广西| 溧阳市| 金塔县| 翁牛特旗| 莱西市| 阳城县| 中江县| 林甸县| 同德县| 扎赉特旗| 天台县|