• js 判断数据类型


    js 判断数据类型,常用4种方法 typeof、instanceof、constructor、Object.prototype.toString.call()

    1. typeof

    1. 一般用来判断基本数据类型
    2. 判断 null 返回 object
    3. 判断引用数据类型,除了function, 其他返回 object, 判断 function 返回 function
    console.log(typeof '123') // string
    console.log(typeof true) // boolean
    console.log(typeof 123) // number
    console.log(typeof undefined) // undefined
    console.log(typeof null) // object
    console.log(typeof { name: '123' }) // object
    console.log(typeof ['123']) // object
    console.log(typeof function () {}) // object
    console.log(typeof /^[a-zA-Z]{5,20}$/) // object
    console.log(typeof new Date()) // object
    // 判断构造函数创建的
    console.log(typeof new Number(123)) // number
    console.log(typeof new String('123')) // string
    console.log(typeof new Boolean(123)) // boolean
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2. instanceof

    1. 一般用来判断引用数据类型
    2. 判断基本数据类型返回 false
    console.log(123 instanceof Number) // false
    console.log('123' instanceof String) // false
    console.log(true instanceof Boolean) // false
    console.log({} instanceof Object) // true
    console.log([] instanceof Array) // true
    console.log(function () {} instanceof Function) // true
    console.log(/^[a-zA-Z]{5,20}$/ instanceof RegExp) // true
    console.log(new Date() instanceof Date) // true
    // 判断构造函数创建的
    console.log(new Number(123) instanceof Number) // true
    console.log(new String('123') instanceof String) // true
    console.log(new Boolean(123) instanceof Boolean) // true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3. constructor

    1. constructor是prototype对象上的属性,指向构造函数。根据实例对象寻找属性的顺序,若实例对象上没有实例属性或方法时,就去原型链上寻找,因此,实例对象也是能使用constructor属性的。
    2. 除了null 和 undefined, 其他类型都可以通过constructor属性来判断类型。
    3. 下面所有结果都是返回true
    console.log('123'.constructor === String)
    console.log((123).constructor === Number)
    console.log(true.constructor === Boolean)
    console.log({ name: '123' }.constructor === Object)
    console.log(['123'].constructor === Array)
    console.log(function () {}.constructor === Function)
    console.log(new Date().constructor === Date)
    console.log(/^[a-zA-Z]{5,20}$/.constructor === RegExp)
    console.log(new Boolean(123).constructor === Boolean)
    console.log(new String('123').constructor === String)
    console.log(new Number(123).constructor === Number)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4. Object.prototype.toString.call() 或者 Object.prototype.toString.apply() 可以精准判断所有的数据类型

    console.log(Object.prototype.toString.call(123)) // [object Number]
    console.log(Object.prototype.toString.call('123')) // [object String]
    console.log(Object.prototype.toString.call(true)) // [object Boolean]
    console.log(Object.prototype.toString.call(null)) // [object Null]
    console.log(Object.prototype.toString.call(undefined)) // [object Undefined]
    console.log(Object.prototype.toString.call({ name: 123 })) // [object Object]
    console.log(Object.prototype.toString.call([123])) // [object Array]
    console.log(Object.prototype.toString.call(function () {})) // [object Function]
    console.log(Object.prototype.toString.call(/^[a-zA-Z]{5,20}$/)) // [object RegExp]
    console.log(Object.prototype.toString.call(new Date())) // [object Date]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    RabbitMQ的介绍
    PL/SQL Developer常规配置分享记录
    Secureboot概念
    Spring 依赖注入
    逆向-beginners之乘法(加法实现)
    机器学习/人工智能的笔试面试题目——NLP相关面试
    2024年测试工程师必看系列之fiddler设置手机端抓包全套教程
    CSS 选择器Day01
    vue-router(路由)详细
    构建精致 Chrome 插件:开箱即用的 TypeScript 模板 | 开源日报 No.51
  • 原文地址:https://blog.csdn.net/weixin_43908123/article/details/127754609