美文网首页
Js数据类型判断封装

Js数据类型判断封装

作者: 光头小青蛙 | 来源:发表于2019-07-19 18:00 被阅读0次

判断数据类型通过typeof,但是typeof只能判断基础类型,判断不了数组对象和null,所以可以利用数组和null的特性来判断数据类型,代码如下。

function check(a) {
            if (typeof a === "string") {
                return "string"
            }
            if (typeof a === "number") {
                return "number"
            }
            if (typeof a === "function") {
                return "function"
            }
            if (typeof a === "undefined") {
                return "undefined"
            }
            if (typeof a === "object" &&!a) {
                return "null"
            }
            if (typeof a === "object"&&a&&a.length>=0) {
                return "array"
            }
            if (typeof a === "object") {
                return "object"
            }
        }

https://juejin.im/post/5d153267e51d4510624f9809

相关文章

网友评论

      本文标题:Js数据类型判断封装

      本文链接:https://www.haomeiwen.com/subject/hwgslctx.html