zoukankan      html  css  js  c++  java
  • undefined null测试

    测试浏览器:chrome
    当有父元素的子元素未定义时undefined和null均为true,类型为undefined
    当元素赋给null后undefined和null均为true,类型为object,因此建议设置为null
    当直接用未定义顶级元素x时,无论是x==undefined或if(x)都会报错
    function log(o){
        console.log(o);
    }
    function lognull(o){
        log(o==undefined)
        log(o==null)
        log(typeof o)
    }  
    var a={aa:'bb'}
    var c=null;
    lognull(a.ff)//未定义子元素不会报错
    lognull(c)
    //以下,未定义顶级元素会报错
    lognull(d)
    if(dd){ //Uncaught ReferenceError: dd is not defined
    }
     
     
     
    测试结果
    true
    true
    undefined
    true
    true
    object
     Uncaught ReferenceError: d is not defined
    -------------------------
     全部代码:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    
    
    <script type="text/javascript">
    function log(o){
        console.log(o);
    }
    function lognull(o){
        log(o==undefined)
        log(o==null)
        log(typeof o)
    }
    function notHave(o){
        if(o==undefined){return true;}
        if(o==null){return true;}
    //    if((typeof o)==undefined){return true;}
        return false;
    }
    
    var a={aa:'bb'}
    var c=null;
    lognull(a.ff)//未定义子元素不会报错
    lognull(c)
    //以下,未定义顶级元素会报错
    lognull(d)
    if(dd){ //Uncaught ReferenceError: dd is not defined
    
    }
    </script>
    </head>
    
    <body>
    </body>
    </html>
    View Code



     

  • 相关阅读:
    平衡树(Splay):Splaytree POJ 3580 SuperMemo
    数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计
    主席树:HDU 4417 Super Mario
    主席树:POJ2104 K-th Number (主席树模板题)
    后缀数组模板
    后缀数组:HDU1043 Longest Common Substring
    后缀数组:SPOJ SUBST1
    关于PHP程序员解决问题的能力
    现在写 PHP,你应该知道这些
    PHP 7 的几个新特性
  • 原文地址:https://www.cnblogs.com/stit/p/6367785.html
Copyright © 2011-2022 走看看