zoukankan      html  css  js  c++  java
  • 阅读javascript高级程序设计随笔(二)

    2.4数据类型

    javascript中有5种基本数据类型:Undefined、Null、Boolean、Number和String。还有一种复杂数据类型Object。

    2.4.1 typeof操作符

    对一个值使用typeof操作符可能返回以下字符串:

    Ⅰ "undefined"   如果这个值没有定义

    Ⅱ "boolean"     如果这个值是布尔值

    Ⅲ "string"        如果这个值是字符串

    Ⅳ "number"    如果职工值是数值

    Ⅴ "object"      如果这个值是对象或者null

    Ⅵ "function"    如果这个值是函数

    如下是typeof操作符的例子:

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title>typeof操作符使用</title>
    </head>
    <body>
    </body>
    <script type="text/javascript">
        var temp = "hello";
        alert(typeof temp);     // 'string'
        alert(typeof (temp));   //'string'
        alert(typeof 100);      //'number'
        alert(typeof a);       //'undefined'
        alert(typeof true);    //'boolean'
    
        var person = {
            name: "Petter"      
        };
        alert(typeof person);       //'object'
        person = null;
        alert(typeof person);       //'object'
    
        function add(){
            alert("a");
        }
        alert(typeof add);       //'function'
    </script>
    </html>

    需要注意:typeof只是一个操作符而不是函数,因此它可以带小括号,也可以不带。Safari5及之前版本、Chrome7及之前版本在对正则表达式调用typeof操作符会返回"function",而其他浏览器在这种情况下会返回"object"。

    2.4.2 undefined类型

    Undefined类型只有一个值,就是undefined。在声明变量时并没有对其进行初始化时,这个变量的值是undefined。当变量没有被初始化时,变量的默认值为undefined。使用没有初始化的变量不会产生错误,但是使用没有定义的变量时,程序会报错。对于没有定义的变量,我们只能进行一个操作(typeof),而且返回undefined值。

    例子:

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title>未定义变量和未初始化变量区别</title>
    </head>
    <body>
    </body>
    <script type="text/javascript">
        var temp;
    
        alert(temp);  //undefined
        alert(a);     //产生一个Uncaught ReferenceError: a is not defined 的错误
        alert(typeof temp); //"undefined"
        alert(typeof a);  //"undefined"
    </script>
    </html>

    2.4.3 Null类型

    Null类型只有一个值,那就是null。从逻辑角度来看,null值表示一个空对象指针,所以使用typeof来检查null会返回"object"。对于用来保存对象的变量,一般初始化为null。

    在进行undefined和null之间的相等操作符总是返回true;

    例子:

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title>未定义变量和未初始化变量区别</title>
    </head>
    <body>
    </body>
    <script type="text/javascript">
        if (null == undefined) {
            alert("null == undefined");
        };
    </script>
    </html>

    3.4.4Boolean类型

    Boolean类型只有两个值,就是true和false。注意是区分大小写的。

    下表给出了各种类型及其对应的转换规则。


     数据类型             转换为true的值                 转换为false的值


    Boolean             true                       false

    String              任何非空字符串                  ""(空字符串)

    Number             任何非零数字(包括无穷大)             0和NaN

    Object              任何对象                     null

    undefined             -----                      undefined


     

  • 相关阅读:
    python初步学习-查看文档及数据类型转换
    python初步学习-python数据类型-集合(set)
    python初步学习-python数据类型-字典(dict)
    python初步学习-python数据类型-列表(list)
    python初步学习-python数据类型之strings(字符串)
    python初步学习-python数据类型之number(数值)
    python初步学习-python运算符
    python初步学习-pycharm使用 (二)
    python初步学习-pycharm使用
    yarn npm 镜像切换
  • 原文地址:https://www.cnblogs.com/night2013/p/3485064.html
Copyright © 2011-2022 走看看