JS实现的判断方法、变量是否存在功能示例

<!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> /*---------------------------判断函数是否存在-------------------------------*/ 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>

本文实例讲述了JS实现的判断方法、变量是否存在功能。分享给大家供大家参考,具体如下:

js 代码中经常会碰到 undefined 这种错误,下面本文分享一下为什么会发生这种错误以及如何处理这种错误,js 中如果通过 var 声明了一个变量但是没有初始化该变量的时候,此时该变量的值便为 undefined ,此时判断变量是否定义可使用 typeof 。下面举例说明一下

if(!result){ alert("发生错误"); }

以上这段代码直接运行会发生异常,因为变量 result 没有申明就被使用了,下面几种写法都是正确的。

(1) if("undefined" == typeof result){ alert("发生错误"); } (2) var result; if(undefined == result){ alert("发生错误"); } (3) if("undefined" == typeof result){ alert("发生错误"); }

补充

例如:

if(!myVar01)alert("发生错误");

// 该代码直接发生异常,因为变量myVar01没有申明 if("undefined" == typeof myVar01)alert("发生错误");

// 这样写才不至于发生异常

而: var myVar01; if(undefined == myVar01)alert("发生错误");

// 该代码会正确运行 if("undefined" == typeof myVar01)alert("发生错误");
// 该代码同样会正确运行

结论:我们采用下面的方式来保证万无一失 if("undefined" == typeof myVar01)alert("发生错误");

// 该代码同样会正确运行

当然判断数据的有效性远远不只这些,还有对null的判断,数字是否大道越界.

实例

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

实际应用:

downlm有的页面我们不定义,但有的页面定义了,就可以需要这样的判断方法,没有定义的就不执行。

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

经测试完美。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/8f3633c014a3dabdc061a2b57b5db58f.html