说一说JavaScript有几种判断数据类型的方法?

判断方法
  1. typeof

  2. instanceof

  3. Object.prototype.toString.call() (对象原型链判断方法)

  4. constructor(用于引用数据类型)

使用方法
  1. typeof

    方法: typeof x || typeof(x)

    return: 数据类型(字符串)

  2. instanceof

    方法: x instanceof 类型(Array || Object || …)

    return: boolean

  3. Object.prototype.toString.call()

    方法: Object.prototype.toString.call(x)

    return: [object 类型] (字符串)

  4. constructor

    方法: x.constructor (x.constructor === 类型)返回boolean值

    return [Function 类型](字符串)

作用范围
  1. typeof

    // 判断基本类型,引用类型不合适
    let number = 1; //数值
    let str = "abc"; //字符串
    let boolean = true;
    let bigint = 123123n; //bigInt类型,数字后面加n ES6新出的数据类型
    const obj = {name:'man杨杨'};
    const arr = [1,2,3,4];
    const func = ()=>{}
    console.log(typeof number);     //number
    console.log(typeof NaN);        //number          
    console.log(typeof bigint);     //bigint
    console.log(typeof str);        //string
    console.log(typeof boolean);    //boolean
    console.log(typeof null);       //object
    console.log(typeof undefined);  //undefined
    console.log(typeof obj);        //object
    console.log(typeof arr);        //object
    console.log(typeof func);       //function
    console.log(typeof(number));    //number
    
    // 结论:
    // 优点:基本类型可以做到很好的区分
    // 缺点:typeof不能区分 null object arr 
    
  2. instanceof

    let number = 1; //数值 字面量创建
    let number1 = new Number(1); //数值 通过new实例创建
    const obj = {name:'man杨杨'};
    const arr = [1,2,3,4];
    console.log(number instanceof Number);  //false 使用字面量创建的基本类型变量 会返回 false
    console.log(number1 instanceof Number); //true
    console.log(obj instanceof Object); //true
    console.log(arr instanceof Array);  //true
    // 总结:
    // instanceof主要用来判断是Array还是Object,不太适合用来判断基本类型
    // instanceof 主要和原型、原型链有关
    // instanceof 主要的实现原理就是只要右边变量的 prototype 在左边变量的原型链上即可。
    
  3. Object.prototype.toString.call()

    let number = 1; //数值
    let str = "abc"; //字符串
    let boolean = true;
    let bigint = 123123n; //bigInt类型,数字后面加n ES6新出的数据类型
    const obj = {name:'man杨杨'};
    const arr = [1,2,3,4];
    const func = ()=>{};
    console.log(Object.prototype.toString.call(number));    //[object Number]
    console.log(Object.prototype.toString.call(str));       //[object String]
    console.log(Object.prototype.toString.call(boolean));   //[object Boolean]
    console.log(Object.prototype.toString.call(bigint));    //[object BigInt]
    console.log(Object.prototype.toString.call(obj));       //[object Object]
    console.log(Object.prototype.toString.call(arr));       //[object Array]
    console.log(Object.prototype.toString.call(func));      //[object Function]
    console.log(Object.prototype.toString.call(new Date()));//[object Date]
    console.log(Object.prototype.toString.call(/a/));       //[object RegExp]
    let t = Object.prototype.toString.call(obj);            //[object Object]
    console.log(Object.prototype.toString.call(t));         //[object String]
    /* 
        总结
        可以判断任意数据类型
    */
    
  4. constructor

    let number = 1; //数值
    let str = "abc"; //字符串
    let boolean = true;
    let bigint = 123123n; //bigInt类型,数字后面加n ES6新出的数据类型
    const obj = {name:'man杨杨'};
    const arr = [1,2,3,4];
    const func = ()=>{};
    console.log(number.constructor === Number);     //true
    console.log(str.constructor === String);        //true
    console.log(boolean.constructor === Boolean);   //true
    console.log(bigint.constructor === BigInt);     //true
    console.log(obj.constructor === Object);        //true
    console.log(arr.constructor === Array);         //true
    console.log(func.constructor === Function);     //true
    /* 
        总结
        因为 null、undefined 是无效的对象,所以他们没有constructor
        constructor 判断类型的限制过多且不准确,不推荐使用
    */
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值