How to check a not defined variable in javascript
javascript里怎么检查一个未定义的变量?
in JavaScript null is an object. There's another value for things that don't exist, undefined. The DOM returns null for almost all cases where it fails to find some structure in the document, but in JavaScript itself undefined is the value used.
Second, no, they are not directly equivalent. If you really want to check for null, do:
if (null == yourvar) // with casting
if (null === yourvar) // without casting
If you want to check if a variable exist
if (typeof yourvar != 'undefined') // Any scope
if (window['varname'] != undefined) // Global scope
if (window['varname'] != void 0) // Old browsers
If you know the variable exists but don't know if there's any value stored in it:
if (undefined != yourvar)
if (void 0 != yourvar) // for older browsers
If you want to know if a member exists independent of whether it has been assigned a value or not:
if ('membername' in object) // With inheritance
if (object.hasOwnProperty('membername')) // Without inheritance
If you want to to know whether a variable autocasts to true:
if(variablename)
原文: http://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript
How to check a not defined variable in javascript的更多相关文章
- Run-Time Check Failure #3 - The variable 'p' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'p' is being used without being initialized. 运行时检查失败 变量p没有初 ...
- 图形化界面安装oracle报错Could not execute auto check for display colors using command /usr/bin/xdpyinfo. Check if the DISPLAY variable is set.
问题描述: 在Linux + oracle 安装时,采有root 帐号登录x-windows 界面,然后 $su oracle 登录录安装Oracle 报以下错误: >>> Coul ...
- [label][JavaScript][The Defined Guide of JavaScript] 变量的作用域
变量的作用域 一个变量的作用域(scope)是程序中定义这个变量的区域. 全局(global)变量的作用域(scope)是全局性的,即在JavaScript代码中,它处处都有定义. 而在函数之内 ...
- [label][JavaScript][The Defined Guide of JavaScript] 如何声明变量
因为觉得我自己的JavaScript基础很不扎实,或者可以说根本就没有所谓基础,所以就最近一直在看<The Defined Guide of JavaScript> . 在一边看的同时,我 ...
- PowerShell Remove all user defined variable in PowerShell
When PS scripts executes, it is possibly create much user defined variables. So, sometimes these var ...
- oracle check if the display variable is set
- What's the difference between using “let” and “var” to declare a variable in JavaScript?
https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare ...
- 2292: Quality of Check Digits 中南多校 暴力枚举
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> ...
- [label][翻译][JavaScript]如何使用JavaScript操纵radio和check boxes
Radio 和 check boxes是form表单中的一部分,允许用户通过鼠标简单点击就可以选择.当与<textarea>元素的一般JavaScript操纵相比较,这些表单控件(form ...
随机推荐
- http请求方式和状态管理
1. HTTP协议 Internet的基本协议是TCP/IP协议(传输控制协议和网际协议),目前广泛使用的 FTP.HTTP(超文本传输协议).Archie Gopher都是建立在TCP/IP上面的 ...
- MFC中菜单栏使用
1.新建项目: 选择MFC应用程序,应用程序类型选择“基于对话框”: 本文中项目名为:MenuTest 2.新建菜单栏资源: 找到资源视图,右键MenuTest.re选择“添加资源”——选择Menu, ...
- POJ 2594 Treasure Exploration(带交叉路的最小路径覆盖)
题意: 派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上,再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.有的点可以重复去. 输入数据: 首先是n和 ...
- MFC 显示CImg图片
很多示例关于CImg都是基于控制台的,如何把它用于MFC中显示. Problem:直接按照控制台示例写入MFC程序中,当程序执行完display后,其后面的代码便不再执行. solution:开辟新的 ...
- FZYZOJ-1880 【UFO】水管
P1880 -- [ufo]水管 时间限制:1000MS 内存限制:131072KB 通过/提交人数:32/100 状态: 标签: 数学问题-组合数学 无 ...
- HOWTO:保存nohup日志
默认情况下,nohup的日志将保存在一个名为nohup.out的文件中.随着时间的推移,nohup.out文件会变得越来越大,直到某一天程序莫名的崩溃.这种情况是可以预防的,有很多策略都可以解决这个问 ...
- 在eclipse下远程调试hadoop2.0
在<在eclipse下编译hadoop2.0源码>一文中,我详细介绍了如何在eclipse环境和命令行环境下编译hadoop2.0源代码,并简单介绍了如何构建hadoop环境,这篇文章将着 ...
- windows路由命令route
route print 查看当前的路由信息 route add 10.0.0.0 mask 255.0.0.0 10.1.1.1 增加一条到10.0.0.0/8网络的路由,网关是10.1.1.1 ro ...
- Java父类子类的对象初始化过程
摘要 Java基本的对象初始化过程,子类的初始化,以及涉及到父类和子类的转化时可能引起混乱的情况. 1. 基本初始化过程: 对于一个简单类的初始化过程是: static 修饰的模块(static变量和 ...
- 在asp.net中使用confirm可以分为两种:
在asp.net中使用confirm可以分为两种: 1.没有使用ajax,confirm会引起也面刷新 2.使用了ajax,不会刷新 A.没有使用ajax,可以用StringBuilder来完成. ( ...