• js中获取数据类型


    ES5中,js中数据类型:number、string、boolean、undefined、null、object

    js中获取数据类型常用的四种方式

    实例:

    var a = 123,
        b = true,
        c = "123",
        d = undefined,
        e = null;
    var o = new Object();
    var f = new Function();
    var f1 = function(){};
    function f2(){}
    var arr = [];
    var arr1 = new Array();
    var reg = new RegExp();  

    1. typeof

     可以判断 js 中基本数据类型,但无法判断对象的具体类型 

    console.log("a:"+typeof(a));
    console.log("b:"+typeof(b));
    console.log("c:"+typeof(c));
    console.log("d:"+typeof(d));
    console.log("e:"+typeof(e));
    console.log("o:"+typeof(o));
    console.log("f:"+typeof(f));
    console.log("f1:"+typeof(f1));
    console.log("f2:"+typeof(f2));
    console.log("arr:"+typeof(arr));
    console.log("arr1:"+typeof(arr1));
    console.log("reg:"+typeof(reg));  

    注意:当使用基本包装类型创建字符串,数组或布尔值时,使用typeof返回的是Object

        判断基本类型

    function ccTypeof(cc){
      return cc === null ? "null" : typrof(cc);
     }  

    2. Object.prototype.toString.call(1)

    可以判断具体的对象类型,包括正则等,但是无法判断自定义对象类型。

    console.log("a:"+ Object.prototype.toString.call(a));
    console.log("b:"+ Object.prototype.toString.call(b));
    console.log("c:"+ Object.prototype.toString.call(c));
    console.log("d:"+ Object.prototype.toString.call(d));
    console.log("e:"+ Object.prototype.toString.call(e));
    console.log("o:"+ Object.prototype.toString.call(o));
    console.log("f:"+ Object.prototype.toString.call(f));
    console.log("f1:"+ Object.prototype.toString.call(f1));
    console.log("f2:"+ Object.prototype.toString.call(f2));
    console.log("arr:"+ Object.prototype.toString.call(arr));
    console.log("arr1:"+ Object.prototype.toString.call(arr1));
    console.log("reg:"+ Object.prototype.toString.call(reg)); 

    function A(){
     this.a = 1;
    }
    var x = new A();
    console.log(Object.prototype.toString.call(x)); 
    

    3. instanceof

    用法:变量 nstaceof 对象,返回值为boolean。

    仅能判断对象的具体类型,但可以拥于判断自定义对象类型。

    var a = 123,
        b = true,
        c = "123";
        //d = undefined,
        //e = null;
    var o = new Object();
    var f = new Function();
    var f1 = function(){};
    function f2(){}
    var arr = [];
    var arr1 = new Array();
    var reg = new RegExp();
    
    console.log(a instanceof Number);
    console.log(b instanceof Boolean);
    console.log(c instanceof String);
    //console.log("d:"+d instanceof Undefined);
    //console.log("e:"+e instanceof Null);
    console.log(o instanceof Object);
    console.log(f instanceof Function);
    console.log(f1 instanceof Function);
    console.log(f2 instanceof Function);
    console.log(arr instanceof Array);
    console.log(arr1 instanceof Array);
    console.log(reg instanceof RegExp);  

     

    function A(){
     this.a = 1;
    }
    function B(){
     this.b = 2;
    }
    var x = new A();
    if(x instanceof A){
      console.log("x is A");   
    }
    if(x instanceof B){
      console.log("x is B");   
    }else{
      console.log("x is not B");  
    }

    4. constructor

    查看对象对应的构造函数

    object的每个实例都具有属性constructor,保存着用于创建当前对象的函数。

    function A(){
     this.a = 1;
    }
    
    var x = new A();
    console.log(x.constructor);

    function A(){
     this.a = 1;
    }
    function B(){
     this.b = 2;
    }
    var x = new A();
    if(x.constructor == A){
      console.log("x is A");   
    }
    if(x.constructor == B){
      console.log("x is B");   
    }else{
      console.log("x is not B");  
    }
    

    但是Undefined和Null类型不能判断

     

    打印所有类型

    function ccTypeof(cc){
      var typeName == Object.prototype.toString.call(cc);
      if( typeName == "[object Object]"){
          typeName = "[object" + cc.constructor.name + "]";
       }
    }
    

    注意:判断数组还可以用数组的isArray()方法,语法:Array.isArray(arr),返回值为Boolean值。

  • 相关阅读:
    AppDelegate动态加载StoryBoard
    带交互的 iOS 产品原型可以用什么软件制作?
    day06.1-module模块和包介绍
    day05.2-一个文件的增删改查实例
    day05.1-文件处理
    day04.4-装饰器
    day04.3-生成器
    day04.2-迭代器
    day04.1-三元表达式与列表解析
    day03.2-内置函数的使用
  • 原文地址:https://www.cnblogs.com/Amy-world/p/9958208.html
Copyright © 2020-2023  润新知