• 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
  • 相关阅读:
    【vue-router】Vue路由从创建到使用
    外设驱动库开发笔记43:GPIO模拟SPI驱动
    mongodb 实现两个集合的关联并分页查询
    计算机毕业设计springboot计算机类专业工程认证资料管理系统+e65c9源码+系统+程序+lw文档+部署
    Laravel 6 - 第十七章 配置数据库
    文件上传及CSRF+Selfxss
    Publisher/Subscriber 订阅-发布模式原理解析
    Telnet协议详解
    Spring Boot配置多个日志文件记录不同类日志示例
    七天强化学习DAY1-1|(一)模型基础
  • 原文地址:https://blog.csdn.net/weixin_43908123/article/details/127754609