zoukankan      html  css  js  c++  java
  • js类型与检查

    标准类型:(6种)

                  原始类型5种(primitive)->(Undefine、Null、Boolean、String、Number)

                  引用类型 1种 Object  eg: var obj = {};

                  js为String、Number、Boolean、Object提供了包装类,这样new出来对象的也是引用类型。

                  eg: var a = new Boolean(true);  var s = new String("hello"); var n = new Number(123); var o = new Object();

                  原始类型和引用类型的区别:

                     

                    复制的差别:

         

                  Undefined(以声明但为赋值,获取对象不存在的属性,无返回值的函数的执行结果,函数的参数没有传入,void()表达式)

                  Undefined可以向Boolean(false)、Number(nan->not a number)、String("undefined")转化

                  Null(获取一个并不存在的对象的时候)

                  Null 向Boolean(false)、Number(0)、String("null")转化。 

                  Boolean(true、false)  转化    true    Number(1)  String("true")

                                                          false   Number(0)  String ("false")

                String(带引号或双引号括起来的字符序列)

                                                          ""      Boolean(false)     Number(0)

                                                        "123"   Boolean(true)      123

                                                         "1a"    Boolean(true)      NAN 

                Number           0                 Boolean(false)      String("0")

                                       1                 Boolean(true)      String("1")

                                   infinity(无穷大)  Boolean(true)      String("infinity")

                                   NAN                Boolean(false)     String("NAN")

               Object(是一组属性的集合)

                                  {}   转化    Boolean(true)  Number(NAN)  String"[object Object]"

    类型识别:typeof  可以识别标准类型,null除外。 不能识别具体的对象类型,function除外。

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
    
        <script>
    
            document.write(typeof(1)+"<br>");
            document.write(typeof("jerry")+"<br>");
            document.write(typeof(true)+"<br>");
            document.write(typeof(undefined)+"<br>");
            document.write(typeof(null)+"<br>");
            document.write(typeof({name:"bella"})+"<br>");
            document.write(typeof(function(){})+"<br>");
            document.write(typeof([])+"<br>");
            document.write(typeof(new Date())+"<br>");
            document.write(typeof(/d/)+"<br>");
            function Person(){
    
            }
            document.write(typeof new Person+"<br>");
            
    
        </script>
    </head>
    <body>
    
    
    
    </body>
    </html>

    结果:

    number
    string
    boolean
    undefined
    object
    object
    function
    object
    object
    object
    object

    Object.prototype.toString

     (1)可以识别标准类型以及内置(build-in)对象类型(2)不能识别自定义类型

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
    
        <script>
            function type(obj){
               return Object.prototype.toString.call(obj).slice(8, -1).toLocaleLowerCase();
           }
           document.write(type(1)+"<br>");
           document.write(type("abc")+"<br>");
           document.write(type(true)+"<br>");
           document.write(type(undefined)+"<br>");
           document.write(type(null)+"<br>");
           document.write(type({})+"<br>");
           document.write(type([])+"<br>");
           document.write(type(new Date())+"<br>");
           document.write(type(/d/)+"<br>");
           document.write(type(function(){})+"<br>");
          function Point(x,y){
              this.x=x;
              this.y=y;
          }
            document.write(type( new Point(1,2))+"<br>");
        </script>
    </head>
    <body>
    
    
    
    </body>
    </html>

    结果:

    number
    string
    boolean
    undefined
    null
    object
    array
    date
    regexp
    function
    object

    constructor 是对象原型上的属性,它指向了构造器本身。 "jerry".constructor === String  //true

    识别标准类型(Undefined/Null除外)

    识别内置对象类型。

    识别自定义对象类型。

    instanceof

     可以判别内置对象类型  [] instanceof Array;   // true

                                  /d/ instanceof RegExp; //true

    不能判别原始类型   1 instanceof Number;  //false

                            "jerry" instanceof String; //false

    可以判别自定义类型

                              function  Point(x,y){

                                    this.x=x;

                                    this.y=y;

                              }           

                              var c = new point(1,2)

                             c instanceof Point  //true

     

    oracle Form 开发
  • 相关阅读:
    爬虫杂七杂八
    pycharm使用技巧
    python杂七杂八
    mysql杂七杂八
    mysql常见函数总结:
    CF1030F Putting Boxes Together
    AT2688 [ARC080C] Young Maids
    P5280 [ZJOI2019]线段树
    雨的味道
    P2572 [SCOI2010]序列操作
  • 原文地址:https://www.cnblogs.com/watson945/p/5067110.html
Copyright © 2011-2022 走看看