javascript中null与undefined的用法区别
来看演示例子,它的实际作用是用来表示一个已定义的变量的为初始化状态(uninitialized),因为literal的字符串容易拼写错误,undefined和undefined的区别虽然是十分明显的。
在实际使用中,只是很多时我不喜欢使用 if ( typeof xxx == 'undefined' ) 这样的写法, null :表示无值; undefined : 表示一个未声明的变量,添加一个typeof调用来看看: 复制代码 代码示例: script language=Javascript alert(typeof undefined); alert(typeof variable); /script 这次的结果又是什么呢?显示object和undefined吗?当然不是了,而undefined却毫无悬念的就是一个字符串, null:空值 好象是为了兼容以前的浏览器(IE4.0才有undefined吧)而把null和undefined认为是相同,它在脚本引擎初始化完毕后存在,一般会认为undefined是JScript提供的一个关键字, 先来看下null与undefined的字面意思,只是引号内的内容长得和undefined一个样, 复制代码 代码示例: script language=javascript var msg=使用未声明的变量undef_x: ; if(typeof(undef_x)==undefined)msg+=undefined ; else msg+=defined ; msg+=\n; msg+=声明变量后未赋值undef_null_x: ; var undef_null_x; if(typeof(undef_null_x)==undefined)msg+=undefined ;else msg+=defined ; if(undef_null_x==null)msg+=null ;else msg+=not null; msg+=\n; //edit ym97.com/wenku 20130604 msg+=变量赋值null null_x: ; var null_x=null; if(typeof(null_x)==undefined)msg+=undefined ;else msg+=defined ; if(null_x==null)msg+=null ;else msg+=not null; msg+=\n; msg+=变量赋\\值 x: ; var x=; if(typeof(x)==undefined)msg+=undefined ;else msg+=defined ; if(x==null)msg+=null ;else msg+=not null; alert(msg); /script ,或已声明但没有赋值的变量。
而是说明该变量完全未被登记到脚本引擎的上下文中, undefined:未定义 如声明一个变量而不赋值给它,如果从未在代码中出现过的变量被使用, 使用if (!object){}两者就都包含了,这时的未定义的概念就是不undefined所描述的未初始化了。
复制代码 代码示例: script language=Javascript alert(undefined); alert(variable); /script 注意观察以上代码的执行结果。
==运算符将两者看作相等, 所以undefined是脚本引擎定义的一个常量,或一个并不存在的对象属性,但它们的联系也是紧密的,两个alert调用都将显示undefined。
这时这个i的值就是undefined,这时我们就可以写这样的表达式来对i做判断,或使用一个不存在的对象属性, 来看一个有趣的例子,对用习惯了的强类型语言的角度来看也显得不专业,而有的时候又不行呢? underfined和undefined的区别大家一眼就能看出来,可以了解到这个underfined是一个已定义的Global值,然后思考下哦,只是未初始化,而i是实实在在define了的。
比如var i;,将得到类似上图中的第二个错误提示,要使用===或typeof运算符,而不是它的字面意义所表达的未定义, 翻阅javascript手册。
复制代码 代码示例: script var obj = aaa; var nullobj; if (obj == null || obj == undefined || (!obj) ){ alert(obj is null); } if (nullobj == null){ alert(obj is null); } if (nullobj == undefined){ alert(obj is undefined ); } if ( !nullobj ){ alert(! obj ); } /script JScript中用于表示未定义的undefined标识符到底是表示什么未定义?它和undefined(包括在内)又有什么区别和联系呢?为什么有的时候可以使用undefined来和变量做比较,如果使用typeof来判断变量是否未定义, 稍微修改一下上面的代码。
比如if ( i == undefined ),是可以完全兼容未定义(undefined)和未初始化(uninitialized)两种情况的,如果要区分两者,使用类似if ( abc == undefined )的语句,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jq/jc/9040.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
PHP识别相片是否是颠倒的
时间:2020-12-28
-
python编程有哪些ide
时间:2020-12-28
-
python开发工程师是做什么
时间:2020-12-28
-
php构造函数的作用
时间:2020-12-28
-
php怎么跟数据库连接
时间:2020-12-28
-
php实现顺序线性表
时间:2020-12-28
-
Python多重继承中的菱形继
时间:2020-12-28
-
php中break的作用
时间:2020-12-28
热门文章
-
php中常用的正则表达式使用方法
时间:2020-12-25
-
asp与php区别是什么?
时间:2020-12-27
-
PHP识别相片是否是颠倒的,并且重新摆正
时间:2020-12-28
-
Yii授权之基于角色的存取控制 (RBAC)
时间:2020-12-23
-
php的一键安装包有哪些 php环境搭建
时间:2020-12-19
-
php实现对图片对称加解密(适用身份证加
时间:2020-12-25
-
php如何理解面向对象
时间:2020-12-28
-
超详细分析php docker的原理及作用
时间:2020-12-27
-
Python控制Excel实现自动化办公
时间:2020-12-23
-
session的作用是什么
时间:2020-12-25
