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'

你可能感兴趣的:(javascript,开发语言,ecmascript)