JS判断数据类型的四种方法(typeof、instanceof、constructor、Object.prototype.toString.call()、testType())

Js数据类型

基本数据类型(值类型):String、Number、Boolean、Null、Undefined、BigInt、Symbol

应用数据类型(引用类型):Object(Function、Array、Date、RegExp、Error等等)

一、typeof

        通常用来判断基本数据类型,它返回表示数据类型的字符串(返回结果只能包括String、Number、Boolean、Null、Undefined、BigInt、Symbol);注意:使用typeof来判断null和引用类型返回的结果都是Object

        可以使用typeof 判断变量是否定义

二、instanceof

        使用instanceof判断参照对象的prototype属性所指向的对象是否在被行测对象的原型链上,instanceof只能用来判断两个对象是否属于实力关系,而不能判断一个对象实力具体属于那种类型。注意:instanceof后面一定要是对象类型,instanceof前面相当于它的实力对象,后面的对象类型大小写不能写错,该方法使用一些条件选择或分支。

三、根据constructor判断

        constructor是原型对象的属性指向构造函数。注意:不能检测undefined和null

四、通过Object.prototype.toString.call()方法来判断

五、testType()方法是我自己总结的

 const testType = (obj) => {
    if (obj === null) return null;
    if (obj === undefined) return undefined;
    switch (typeof obj) {
      case "object":
        // console.log("Object--->", Object.prototype.toString.call(obj));
        switch (Object.prototype.toString.call(obj)) {
          case "[object Date]":
            return "Date";
          case "[object Function]":
            return "Function";
          case "[object Object]":
            return "Object";
          case "[object Array]":
            return "Array";
          case "[object RegExp]":
            return "RegExp";
          case "[object Error]":
            return "Error";
        }
        break;
      default:
        return typeof obj;
    }
  };
  let str = "foo";
  console.log(
    str,
    typeof str,
    str instanceof String,
    str.constructor == String,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // foo string false true [object String] string

  str = 12;
  console.log(
    str,
    typeof str,
    str instanceof Number,
    str.constructor == Number,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // 12 'number' false true '[object Number]' 'number'

  str = true;
  console.log(
    str,
    typeof str,
    str instanceof Boolean,
    str.constructor == Boolean,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // true 'boolean' false true '[object Boolean]' 'boolean'

  str = [1, 2, 3];
  console.log(
    str,
    typeof str,
    str instanceof Array,
    str.constructor == Array,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // (3) [1, 2, 3] 'object' true true '[object Array]' 'Array'

  str = { name: "hahaha", age: 18 };
  console.log(
    str,
    typeof str,
    str instanceof Object,
    str.constructor == Object,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // {name: 'hahaha', age: 18} 'object' true true '[object Object]' 'Object'

  str = function () {
    console.log("function");
  };
  console.log(
    typeof str,
    str instanceof Function,
    str.constructor == Function,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // function true true [object Function] function

  str = undefined;
  console.log(
    str,
    typeof str,
    str instanceof Object,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // undefined 'undefined' false '[object Undefined]' undefined

  str = null;
  console.log(
    str,
    typeof str,
    str instanceof Object,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // null 'object' false '[object Null]' null

  console.log("typeof arr=" + typeof arr);
  // typeof arr=undefined
  str = new Date("2023-06-01");
  console.log(
    str,
    typeof str,
    str instanceof Object,
    str.constructor == Object,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // Thu Jun 01 2023 08:00:00 GMT+0800 (中国标准时间) 'object' true false '[object Date]' 'Date'

  str = /^w$/g;
  console.log(
    str,
    typeof str,
    str instanceof Object,
    str.constructor == Object,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // /^w$/g 'object' true false '[object RegExp]' 'RegExp'

  str = new Error();
  console.log(
    str,
    typeof str,
    str instanceof Object,
    str.constructor == Object,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // Error at index.html:157:9 'object' true false '[object Error]' 'Error'

  str = Symbol();
  console.log(
    str,
    typeof str,
    str instanceof Object,
    str.constructor == Object,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // Symbol() 'symbol' false false '[object Symbol]' 'symbol'

  str = BigInt(123);
  console.log(
    str,
    typeof str,
    str instanceof Object,
    str.constructor == Object,
    Object.prototype.toString.call(str),
    testType(str)
  );
  // 123n 'bigint' false false '[object BigInt]' 'bigint'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值