JS实现的判断方法、变量是否存在功能示例
程序员文章站
2022-04-29 16:33:12
本文实例讲述了js实现的判断方法、变量是否存在功能。分享给大家供大家参考,具体如下:
本文实例讲述了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>
更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript字符与字符串操作技巧总结》、《javascript遍历算法与技巧总结》、《javascript查找算法技巧总结》、《javascript数学运算用法总结》、《javascript数据结构与算法技巧总结》及《javascript错误与调试技巧总结》
希望本文所述对大家javascript程序设计有所帮助。