zoukankan      html  css  js  c++  java
  • JavaScript判断各种数据类型

    • typeof ,只可判断部分数据的数据类型

      • 数字
      • 字符串
      • 布尔值
      • undefined
      • function
    • Object.prototype.toString.call()

    • instanceof

    function judgeType(data) {
        // 不传参,默认undefined
        if (data === undefined) { return "undefined" }
        if (data === null) { return "null" }
        
        const type = Object.prototype.toString.call(data)
        return type.match(/^[object (w+)]$/)[1]
    }
    
    judgeType(1)     // Number
    judgeType('abc') // String
    judgeType(true)  // Boolean
    judgeType(undefined) // Undefined
    judgeType(null)  // Null
    judgeType({})   // Object
    judgeType([])   // Array
    judgeType(window)   // "Window"
    
  • 相关阅读:
    day39
    day36
    day35
    day34
    深入理解css的margin
    git使用
    java常见的分页实现方式
    jquery常识
    与border不得不说的故事
    测试效果
  • 原文地址:https://www.cnblogs.com/guojiabing/p/10601333.html
Copyright © 2011-2022 走看看