最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

怎样使用JS判断变量是否存在

来源:动视网 责编:小采 时间:2020-11-27 19:44:43
文档

怎样使用JS判断变量是否存在

怎样使用JS判断变量是否存在:这次给大家带来怎样使用JS判断变量是否存在,使用JS判断变量是否存在的注意事项有哪些,下面就是实战案例,一起来看一下。<!DOCTYPE html> <html lang="en"> <head> <meta charset="UT
推荐度:
导读怎样使用JS判断变量是否存在:这次给大家带来怎样使用JS判断变量是否存在,使用JS判断变量是否存在的注意事项有哪些,下面就是实战案例,一起来看一下。<!DOCTYPE html> <html lang="en"> <head> <meta charset="UT


这次给大家带来怎样使用JS判断变量是否存在,使用JS判断变量是否存在的注意事项有哪些,下面就是实战案例,一起来看一下。

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

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

怎样实现微信小程序的自定义多选事件

在不使用select的情况下vue怎么实现下拉框功能

文档

怎样使用JS判断变量是否存在

怎样使用JS判断变量是否存在:这次给大家带来怎样使用JS判断变量是否存在,使用JS判断变量是否存在的注意事项有哪些,下面就是实战案例,一起来看一下。<!DOCTYPE html> <html lang="en"> <head> <meta charset="UT
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top