js判断方法变量是否存在
来源:互联网 发布:js给集合添加元素 编辑:程序博客网 时间:2024/05/17 01:44
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body>//http://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>
0 0
- js判断方法变量是否存在
- js 判断变量是否存在
- js判断指定函数、变量是否存在的方法
- js 判断js函数、变量是否存在
- js 判断js函数、变量是否存在
- js 判断js函数、变量是否存在
- js 判断js函数、变量是否存在
- 判断变量是否存在
- js 判断js函数、变量是否存在
- js判断一个变量或对象是否存在
- 判断变量是否存在(python)
- javascript判断变量是否存在
- JS检查变量是否存在
- js 检测变量是否存在
- js判断字符串中是否存在某些字符的方法
- [JavaScript]判断JS中的函数(方法)是否存在
- 用js判断用户名cookie是否存在的正确方法
- JS中typeof 判断方法、对象是否存在
- React 精华一页纸
- 迷宫的最短路问题(水+BFS宽搜)
- C# 操作Sql Server 学习总结
- AOP-AspectJ-基于XML的实现——各种通知
- redis加减操作
- js判断方法变量是否存在
- 设计一个具有GetMin功能的栈
- Redis实现高并发下的抢购、秒杀功能
- 素数打表(小模板)
- 【ES6】16.generator函数的语法 10.(1)异步操作的同步化表达
- 3天速成混合app开发_01环境部署
- 排序算法
- ZOJ3785-What day is that day?
- EventBus使用详解