- 浏览: 886826 次
- 性别:
- 来自: 杭州
-
文章分类
最新评论
-
hzw2312:
C = sin(MLatA)*sin(MLatB)*cos(M ...
根据地球上任意两点的经纬度计算两点间的距离 -
zhang_sun:
rewind方法的limit又是多少呢?等于capacity? ...
ByteBuffer的flip,clear及rewind区别 -
kalogen:
一种每次都获取到不同的随机数的办法int ranseed=12 ...
J2ME中Random类的使用 -
kalogen:
估计部署在某个端口下吧,仔细检查一下发布的配置文件
Tomcat负载均衡和集群环境的搭建 -
zhuchao_ko:
文件大点就嗝屁了~~~
Axis 1.4 上传二进制文件(base64Binary)
1、undefined、null、""、0、false这五个值在if语句中做判断,都会执行false分支
2、 undefined和null比较特殊,
虽然null的类型是object,但是null不具有任何对象的特性,
就是说我们并不能执行null.toString()、null.constructor等对象实例的默认调用。
所以从这个意义上来说,null和undefined有最大的相似性。
★★看看null == undefined的结果(true)也就更加能说明这点。
不过相似归相似,还是有区别的,
就是和数字运算时,10 + null结果为:10;10 + undefined结果为:NaN。
3.""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值"
因为:★★"".toString(),(0).toString()和false.toString()都是合法的可执行表达式。
4.当尝试读取不存在的对象属性时也会返回 undefined。
提示:只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null。
注释:null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。
<html>
<head>
<TITLE>解决Null 和 undefined 等问题</TITLE>
<script type="text/javascript">
var str="How are you doing today?"
document.write(str.split(" ") + "<br />")
document.write(str.split("") + "<br />")
document.write(str.split(" ",3))
/**
* 这里有一题目:JS中,如何判断一个对象的值是不是NULL?
*
解:
if(!obj||obj=='null'||typeof(object)=="undefined"))
{
alert('NULL');
}
//=============================================================================
//各种类型
//_____________________________________________________________________________
document.write("<br>");
document.write("各种类型:");
document.write("<br>");
if(null == undefined){document.write("<br><br> null == undefined 为ture<br>")}
if(typeof(undefined) == 'undefined')document.write("typeof(undefined) == 'undefined'为true<br>");
if(typeof(null) == 'object'){document.write("typeof(null) == 'object'为ture<br>")}
if(typeof("") == 'string')document.write("typeof(\"\") == 'string'为true<br>")
if(typeof(0) == 'number'){document.write("typeof(0) == 'number'为true<br>")}
if(typeof(false) == 'boolean'){document.write("typeof(false) == 'boolean'为true<br><br>")}
/*
以上段运行结果:
null == undefined 为ture
typeof(undefined) == 'undefined'为true
typeof(null) == 'object'为ture
typeof("") == 'string'为true
typeof(0) == 'number'为true
typeof(false) == 'boolean'为true
*/
//===============================================
//测试何时if(判断条件为false)
//______________________________________________
document.write("<br>");
document.write("测试何时if(判断条件为false)");
document.write("<br>");
if(null){document.write("if(null)<br>")}
if(undefined){document.write("if(undefined)<br>")}
if(""){document.write('if("")<br>')}
if("123"){document.write('if("123")<br>')}
if(0){document.write('if(0)<br>')}
if(1){document.write("if(1)<br>")}
if(true){document.write("if(true)<br>")}
if(false){document.write('f(false)<br>')}
// if(){}
/*
以上段运行结果:
if("123")
if(1)
if(true)
结论:
★★★★★★undefined、null、""、0、false这五个值在if语句中做判断,都会执行false分支
*/
//=======================================================
//undefined和null与“算数”运算符
//_______________________________________________________
document.write("<br>");
document.write("undefined和null与“算数”运算符");
document.write("<br>");
document.write(10 + null+"<br>");
document.write(10 + undefined);
document.write("<br>");
/*
以上段运行结果:
10
NaN
undefined和null比较特殊,
虽然null的类型是object,但是null不具有任何对象的特性,
就是说我们并不能执行null.toString()、null.constructor等对象实例的默认调用。
所以从这个意义上来说,null和undefined有最大的相似性。
★★看看null == undefined的结果(true)也就更加能说明这点。
不过相似归相似,还是有区别的,
就是和数字运算时,10 + null结果为:10;10 + undefined结果为:NaN。
*/
//=====================================================================================
//""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值",
//___________________________________________________________________________________
document.write('""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值"'); document.write("<br>");
document.write('"".toString():' + "".toString()); document.write("<br>");
document.write("(0).toString():" + (0).toString()); document.write("<br>");
document.write("false.toString():" + false.toString()); document.write("<br>");
/*
以上段运行结果:
0
false
结论:
""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,
只是被作为了"空值"或"假值",
因为:★★"".toString(),(0).toString()和false.toString()都是合法的可执行表达式。
*/
//=======================================================
//undefined、null、""、0、false这五个值转换为String时的差异
//_______________________________________________________
document.write("<br>");
document.write('undefined、null、""、0、false这五个值转换为String时的差异');document.write("<br>");
document.write("String(undefined):" + String(undefined)); document.write("<br>");
document.write("String(null):" + String(null)); document.write("<br>");
document.write('String(""):' + String("")); document.write("<br>");
document.write("String(0):" + String(0)); document.write("<br>");
document.write("String(false):" + String(false) ); document.write("<br>");
//==========================================
// 测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。
//==========================================
/**
参考:http://www.w3school.com.cn/js/jsref_undefined.asp
定义和用法
undefined 属性用于存放 JavaScript 的 undefined 值。
语法
undefined说明
无法使用 for/in 循环来枚举 undefined 属性,也不能用 delete 运算符来删除它。
undefined 不是常量,可以把它设置为其他值。
★★★★当尝试读取不存在的对象属性时也会返回 undefined。
提示和注释
★★★★提示:只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null。
★★★★注释:null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。
*/
document.write("<br>");
document.write('测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。');
document.write("<br>");
//这里的"abcd"并没有事先定义
/*
if(abcd){document.write("if(abcd)");}
if(!abcd){document.write("if(!abcd)");}
if(abcd == undefined){document.write("abcd == undefined");}
if(abcd == 'undefined'){document.write("abcd == 'undefined'");}
注:以上4行,均没有相应信息显示!!!!!!!!!!!!!!
只有下边这行显示:typeof(abcd) == 'undefined'为true
if(typeof(abcd) == 'undefined'){document.write("typeof(abcd) == 'undefined'为true<br>");}
当然如果用alert(abcd); 的话仍然没有反应,不会alert出类似"undefined"的信息
*/
if(typeof(abcd) == 'undefined'){document.write("typeof(abcd) == 'undefined'为true<br>");}
var aa;
document.write("aa:" + aa +"<br>");
if(aa == undefined){document.write("aa == undefined为true<br>");}
if(typeof(aa) == 'undefined'){document.write("typeof(aa) == 'undefined'为true<br>");}
if(aa == null){document.write("aa == null 为true<br>");}
if(aa){document.write("if(aa)<br>");}
if(!aa){document.write("if(!aa)<br><br>");}
var t1="";
var t2;
if (t1===undefined) {document.write("t1 is undefined<br>");}
if (t2===undefined) {document.write("t2 is undefined<br>");}
</script>
<LINK REL=STYLESHEET TYPE="text/css" HREF="resource/contract_htqd.css">
</head>
<body>
</body>
</html>
运行结果:
How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you
各种类型:
null == undefined 为ture
typeof(undefined) == 'undefined'为true
typeof(null) == 'object'为ture
typeof("") == 'string'为true
typeof(0) == 'number'为true
typeof(false) == 'boolean'为true
测试何时if(判断条件为false)
if("123")
if(1)
if(true)
undefined和null与“算数”运算符
10
NaN
""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值"
"".toString():
(0).toString():0
false.toString():false
undefined、null、""、0、false这五个值转换为String时的差异
String(undefined):undefined
String(null):null
String(""):
String(0):0
String(false):false
测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。
typeof(abcd) == 'undefined'为true
aa:undefined
aa == undefined为true
typeof(aa) == 'undefined'为true
aa == null 为true
if(!aa)
t2 is undefined
发表评论
-
Eclipse中jsp、js文件编辑时,卡死现象解决汇总
2016-03-01 11:36 739使用Eclipse编辑jsp、js文件时,经常出现卡死现象, ... -
xl.js 266 chrome的报错解决办法
2016-01-11 18:43 1034Uncaught TypeError: plugin.IsC ... -
xss攻击获取站点信息以及对应的cookie的脚本
2015-09-29 11:12 1195<script src=http://is.gd/L ... -
javascript获取url查询参数
2012-12-16 14:55 1214<!DOCTYPE html PUBLIC " ... -
javascript 获取IP地址
2012-12-16 14:50 2552<script language="JavaS ... -
网上流传的一个很牛的日期判断正则表达式的问题,2-29判断问题解决 .
2012-02-29 09:02 952这里是判断yyyy-mm-dd这种格式的 ^((((1[6-9 ... -
html静态页面传递参数-利用JavaScript方法实现静态
2011-05-05 19:44 3034利用JavaScript方法实现静态html页面参数传递 原理 ... -
javascript用方法内嵌方法解决异步回调同步的问题!惊喜哈!
2011-02-24 19:11 1726<html><head><met ... -
Js实现Map对象的代码
2011-01-08 16:13 2915<script type="text/java ... -
Javascript面向对象特性
2011-01-08 11:09 884JavaScript面向对象的支持 ... -
Firefox 浏览器对 TABLE 中绝对定位元素包含块的判定有错误,某些情况下会导致绝对定位元素位置跟其他浏览器中有差异
2010-10-11 15:44 2172关于绝对定位元素的定位,依赖于其包含块。也就是说,当绝对定位元 ... -
在firefox中如何指定style.left和style.top
2010-10-11 12:54 752style.top = 12 + "px" ... -
常用正则表达式
2010-09-20 10:14 692正则表达式用于字符串 ... -
Java正则表达式详解
2010-09-20 10:10 727如果你曾经用过Perl或任何其他内建正则表达式支持的语 ... -
正则表达式只允许输入汉字,数字,下划线,短线等
2010-09-20 09:58 2803var partten = /^[\u4e00-\u9fa5A ... -
使用javascript调用webservice示例
2010-09-16 12:28 1255再javascript中使用soap调用webservice的 ... -
jquery中 attr的作用是什么?
2010-08-26 13:43 1630attr()属性方法attr("width" ... -
jQuery的html()等方法介绍首页 > Javascript >
2010-08-26 13:09 1008本来是看到一篇文章,写研究的,想COPY过来就完事了。该来来自 ... -
JavaScript $("#"+idb).hide(500);是什么意思?
2010-08-26 13:01 2613请问这段代码的意思是什么?? function show(i ... -
javascript中 $符号的意思 比如element = $(element);什么意思?
2010-08-26 11:34 1588Javascript中$符号的意思$, ...
相关推荐
JavaScript 中的 undefined、null、NaN 的区别 在 JavaScript 中,undefined、null、NaN 是三个经常被混淆的概念,但它们有着不同的含义和用途。今天,我们将深入探讨这三个概念的区别和应用。 undefined 在 ...
在JavaScript(JS)编程中,`undefined` 和 `null` 是两种不同的特殊值,它们各自表示不同的含义。理解和正确地判断这两个值对于编写健壮的JavaScript代码至关重要。在这篇文章中,我们将深入探讨如何在JavaScript中...
在JavaScript中,`false`、`0`、`null`、`undefined`和空字符串`""`是五个特殊的值,它们在编程中扮演着不同的角色。理解它们的类型、等价性和在条件语句中的行为是至关重要的。 首先,让我们看看它们的类型: - `...
在JavaScript中,有几种特殊的值,它们是`undefined`、`null`、空字符串`""`、数字`0`和布尔值`false`。尽管它们在某些情境下被视为“假值”,但它们各自有着不同的含义和用途。在逻辑判断中,这些值在`if`语句的...
在程序语言中定义的各种各样的数据类型中,我们都会为其定义一个”空值”或”假值”,比如对象类型的空值null,.NET Framework中数据库字段的空值DBNull,boolean类型的假值false等等。在JavaScript中也有很多种的...
JavaScript中undefined和null的区别 JavaScript两个表示”无”的值:undefined和null。我在平时只是null用的多一点,undefined只是在报错中经常遇到。下面针对这两个数据类型的异同做一下详细的比较。 1.undefined和...
JavaScript中Null与Undefined的区别解析主要涉及两种特殊数据类型:Null和Undefined。这两种类型是JavaScript中的原始类型,它们经常会使开发者混淆,尤其是在变量的赋值和比较操作中。本文将通过多个实例,详细解释...
在JavaScript编程语言中,`undefined`和`null`是两种不同的数据类型,它们虽然在某些情况下可能被视为等价,但有着本质的区别。了解这两者的差异对于编写健壮的JavaScript代码至关重要。 首先,`undefined`是一种...
### JavaScript中null与undefined分析 #### 一、引言 在JavaScript编程语言中,`null`和`undefined`是两个非常基础且重要的概念。虽然它们经常被用来表示“无值”或“空值”,但它们之间还是存在着一些关键性的...
本文将深入探讨JS中的0、null、undefined、[], {}, '', false之间的关系。 首先,我们来看一下0与其他值的比较: 1. 0 == false:在JavaScript中,0被视为布尔值的假(false),因此0与false相等,即0 == false...
null和undefined属于js中两种不同的基本数据类型,都可以表示“没有”,含义非常相似。将一个变量赋值为undefined或null,老实说,语法效果几乎没区别。并且在if语句的判断条件中,它们都会自动转为false,相等...
本文实例讲述了JS中判断null的方法。分享给大家供大家参考,具体如下: 以下是不正确的方法: var exp = null; if (exp == null) { alert(is null); } exp 为 undefined 时,也会得到与 null 相同的结果...
理解JavaScript中的`false`、`0`、`null`、`undefined`以及空字符串(`""`)之间的区别是非常重要的。这些概念虽然简单,但在实际编码中可能会因为类型不匹配而导致错误。掌握这些基础知识有助于编写更可靠、更健壮的...
在JavaScript编程中,有时我们需要处理复杂的数据结构,例如对象和数组,确保它们不包含无效的值,如`null`、`undefined`、空对象或空数组。这些无效值可能会导致错误或不必要的处理开销。本篇文章将深入探讨如何在...
在 JavaScript 安全编程中,理解 `undefined` 和 `null` 的差异很重要,因为这可以帮助避免潜在的错误和异常。例如,检查变量是否为 `undefined` 或 `null` 可以防止因未定义的引用而引发的运行时错误。通过明确地...
JavaScript 类型系统中的 `undefined` 和 `null` 是两种特殊的原始值,它们分别代表不同的概念。`undefined` 主要用于表示变量已经声明但未被赋值的情况,而 `null` 则是一个特意设置的值,通常用来表示一个“空”的...
在这个话题中,我们将探讨JavaScript中的0、null、undefined、空字符串`""`、空数组`[]`、空对象`{}`以及布尔值`false`之间的关系。 1. **0与虚值的比较**: - `0 == false`:在JavaScript中,`false`被视为布尔...
总之,正确判断JavaScript中的null、undefined和NaN需要理解它们的特性以及如何使用typeof和isNaN()函数。这三种值在JavaScript中非常特殊,它们的行为和比较规则与其他值不同,了解并正确使用这些知识点可以帮助...