MY 总结:理解js中的:Null、undefined、""、0、false

MY 总结:理解js中的:Null、undefined、""、0、false 

 

总结:

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 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。

 

-----------------------------------------------------------------------------------

书籍资料:

《JavaScript核心技术》机械工业出版社 2007年6月 第一版第一次印刷

0、""、NaN、null和defined都是假的 。剩下的东西都是真的。

换句话说,零、null、NaN和空字符串天生就是假 ;而其他的天生就是真 。

================================================

 

测试实例:

  1. <html>
  2. <head>
  3.     <TITLE>解决Null 和 undefined 等问题</TITLE>
  4.     <script type= "text/javascript" >
  5.     
  6.      var  str= "How are you doing today?"
  7.     
  8.     document.write(str.split( " " ) +  "<br />" )
  9.     document.write(str.split( "" ) +  "<br />" )
  10.     document.write(str.split( " " ,3))
  11.     
  12.   /**
  13.     * 这里有一题目:JS中,如何判断一个对象的值是不是NULL?
  14.     *
  15.         解:
  16.         if(!obj||obj=='null'||typeof(object)=="undefined"))   
  17.           {   
  18.                     alert('NULL');   
  19.             
  20.           }  
  21.           
  22.     网络资源路径:
  23.     http://topic.csdn.net/t/20031230/12/2617647.html
  24.     *
  25.     *
  26.     */
  27.     
  28.     
  29.     
  30.     
  31.     
  32.      //=============================================================================
  33.      //各种类型
  34.      //_____________________________________________________________________________
  35.     document.write( "<br>" );
  36.     document.write( "各种类型:" );
  37.     document.write( "<br>" );
  38.      if ( null  == undefined){document.write( "<br><br> null == undefined 为ture<br>" )}
  39.      if ( typeof (undefined) ==  'undefined' )document.write( "typeof(undefined) == 'undefined'为true<br>" );
  40.      if ( typeof ( null ) ==  'object' ){document.write( "typeof(null) == 'object'为ture<br>" )}
  41.      if ( typeof ( "" ) ==  'string' )document.write( "typeof(/"/") == 'string'为true<br>" )
  42.      if ( typeof (0) ==  'number' ){document.write( "typeof(0) == 'number'为true<br>" )}
  43.      if ( typeof ( false ) ==  'boolean' ){document.write( "typeof(false) == 'boolean'为true<br><br>" )}
  44.      /*
  45.     以上段运行结果:
  46.     null == undefined 为ture
  47.     typeof(undefined) == 'undefined'为true
  48.     typeof(null) == 'object'为ture
  49.     typeof("") == 'string'为true
  50.     typeof(0) == 'number'为true
  51.     typeof(false) == 'boolean'为true
  52.     */
  53.      //===============================================
  54.      //测试何时if(判断条件为false)
  55.      //______________________________________________
  56.     document.write( "<br>" );
  57.     document.write( "测试何时if(判断条件为false)" );
  58.     document.write( "<br>" );
  59.      if ( null ){document.write( "if(null)<br>" )}
  60.      if (undefined){document.write( "if(undefined)<br>" )}
  61.      if ( "" ){document.write( 'if("")<br>' )}
  62.      if ( "123" ){document.write( 'if("123")<br>' )}
  63.      if (0){document.write( 'if(0)<br>' )}
  64.      if (1){document.write( "if(1)<br>" )}
  65.      if ( true ){document.write( "if(true)<br>" )}
  66.      if ( false ){document.write( 'f(false)<br>' )}
  67.      //  if(){}
  68.      /*
  69.     以上段运行结果:
  70.     if("123")
  71.     if(1)
  72.     if(true)
  73.     
  74.     结论:
  75.     ★★★★★★undefined、null、""、0、false这五个值在if语句中做判断,都会执行false分支
  76.     */
  77.      //=======================================================
  78.      //undefined和null与“算数”运算符
  79.      //_______________________________________________________
  80.     
  81.     document.write( "<br>" );
  82.     document.write( "undefined和null与“算数”运算符" );
  83.     document.write( "<br>" );
  84.     document.write(10 +  null + "<br>" );
  85.     document.write(10 + undefined);
  86.     document.write( "<br>" );
  87.      /*
  88.     以上段运行结果:
  89.     10
  90.     NaN 
  91.     
  92.     undefined和null比较特殊,
  93.     虽然null的类型是object,但是null不具有任何对象的特性,
  94.     就是说我们并不能执行null.toString()、null.constructor等对象实例的默认调用。
  95.     所以从这个意义上来说,null和undefined有最大的相似性。
  96.     ★★看看null == undefined的结果(true)也就更加能说明这点。
  97.     不过相似归相似,还是有区别的,
  98.     就是和数字运算时,10 + null结果为:10;10 + undefined结果为:NaN。
  99.     */
  100.      //=====================================================================================
  101.      //""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值",
  102.      //___________________________________________________________________________________
  103.     document.write( '""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值"' );  document.write( "<br>" );
  104.     document.write( '"".toString():'  +  "" .toString());   document.write( "<br>" );
  105.     document.write( "(0).toString():"  + (0).toString()); document.write( "<br>" ); 
  106.     document.write( "false.toString():"  +  false .toString()); document.write( "<br>" );
  107.      /*
  108.     以上段运行结果:    
  109.     0
  110.     false
  111.     
  112.     结论:
  113.     ""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,
  114.     只是被作为了"空值"或"假值",
  115.     因为:★★"".toString(),(0).toString()和false.toString()都是合法的可执行表达式。
  116.     */
  117.      //=======================================================
  118.      //undefined、null、""、0、false这五个值转换为String时的差异
  119.      //_______________________________________________________
  120.     document.write( "<br>" );
  121.     document.write( 'undefined、null、""、0、false这五个值转换为String时的差异' );document.write( "<br>" );
  122.     document.write( "String(undefined):"  + String(undefined));   document.write( "<br>" );
  123.     document.write( "String(null):"  + String( null )); document.write( "<br>" );
  124.     document.write( 'String(""):'  + String( "" )); document.write( "<br>" );
  125.     document.write( "String(0):"  + String(0));   document.write( "<br>" );
  126.     document.write( "String(false):"  + String( false ) );  document.write( "<br>" );
  127.      //==========================================
  128.      //  测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。
  129.      //==========================================
  130.   /**
  131.         参考:http://www.w3school.com.cn/js/jsref_undefined.asp
  132.         定义和用法
  133.                 undefined 属性用于存放 JavaScript 的 undefined 值。
  134.         
  135.         语法
  136.                 undefined说明
  137.                 无法使用 for/in 循环来枚举 undefined 属性,也不能用 delete 运算符来删除它。
  138.         
  139.         undefined 不是常量,可以把它设置为其他值。
  140.         
  141.         ★★★★当尝试读取不存在的对象属性时也会返回 undefined。
  142.         提示和注释
  143.                 ★★★★提示:只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null。
  144.                 ★★★★注释:null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。
  145.     */
  146.     document.write( "<br>" );
  147.     document.write( '测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。' );
  148.     document.write( "<br>" );
  149. //这里的"abcd"并没有事先定义
  150. /*
  151.     if(abcd){document.write("if(abcd)");}
  152.     if(!abcd){document.write("if(!abcd)");}
  153.     if(abcd == undefined){document.write("abcd == undefined");}
  154.     if(abcd == 'undefined'){document.write("abcd == 'undefined'");}
  155. 注:以上4行,均没有相应信息显示!!!!!!!!!!!!!!
  156. 只有下边这行显示:typeof(abcd) == 'undefined'为true
  157.     if(typeof(abcd) == 'undefined'){document.write("typeof(abcd) == 'undefined'为true<br>");}
  158.     
  159.     当然如果用alert(abcd); 的话仍然没有反应,不会alert出类似"undefined"的信息
  160. */
  161.      if ( typeof (abcd) ==  'undefined' ){document.write( "typeof(abcd) == 'undefined'为true<br>" );}
  162.      var  aa;
  163.     document.write( "aa:"  + aa + "<br>" );
  164.      if (aa == undefined){document.write( "aa == undefined为true<br>" );}
  165.      if ( typeof (aa) ==  'undefined' ){document.write( "typeof(aa) == 'undefined'为true<br>" );}
  166.      if (aa ==  null ){document.write( "aa == null 为true<br>" );} 
  167.      if (aa){document.write( "if(aa)<br>" );}
  168.      if (!aa){document.write( "if(!aa)<br><br>" );}
  169.      var  t1= "" ;
  170.      var  t2;
  171.      if  (t1===undefined) {document.write( "t1 is undefined<br>" );}
  172.      if  (t2===undefined) {document.write( "t2 is undefined<br>" );}
  173.     </script>
  174.     <LINK REL=STYLESHEET TYPE= "text/css"  HREF= "resource/contract_htqd.css" >
  175. </head>
  176. <body>
  177. </body>
  178. </html>

 

运行结果:

  1. How,are,you,doing,today?
  2. H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
  3. How,are,you
  4. 各种类型:
  5. null  == undefined 为ture
  6. typeof (undefined) ==  'undefined'true
  7. typeof ( null ) ==  'object' 为ture
  8. typeof ( "" ) ==  'string'true
  9. typeof (0) ==  'number'true
  10. typeof ( false ) ==  'boolean'true
  11. 测试何时 if (判断条件为 false
  12. if ( "123" )
  13. if (1)
  14. if ( true )
  15. undefined和 null 与“算数”运算符
  16. 10
  17. NaN
  18. "" 、0和 false 虽然在 if 语句表现为 "假值" ,可它们都是有意义数据,只是被作为了 "空值""假值"
  19. "" .toString():
  20. (0).toString():0
  21. false .toString(): false
  22. undefined、 null"" 、0、 false 这五个值转换为String时的差异
  23. String(undefined):undefined
  24. String( null ): null
  25. String( "" ):
  26. String(0):0
  27. String( false ): false
  28. 测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。
  29. typeof (abcd) ==  'undefined'true
  30. aa:undefined
  31. aa == undefined为 true
  32. typeof (aa) ==  'undefined'true
  33. aa ==  null  为 true
  34. if (!aa)
  35. t2  is  undefined
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值