js封装数据类型判断插件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fly18702787810/article/details/79946375
(function (root,fc) {
    root.$ = fc();  //对外暴露全局变量 $   (root相当于 window)
    return fc;
})(this,function () {
    var util = {};
    var class2type = {};
    var typeStr = "String,Number,Boolean,Array,Object,Date,RegExp,Function,Error,Null,Undefined";
    var util = {
        isArray:function (obj) {
            return getType( obj ) === 'array'
        },
        isNumber:function (obj) {
            return getType( obj ) === 'number'
        },
        isBoolean:function (obj) {
            return getType( obj ) === 'boolean'
        },
        isString:function (obj) {
            return getType( obj ) === 'string'
        },
        isFunction:function (obj) {
            return getType( obj ) === 'function'
        },
        isNull:function (obj) {
            return getType( obj ) === 'null'
        }
    };
    function getType( obj ) {
        if ( obj == null ) {     //兼容IE6  ,typeof undefined 等于了 [Object Object]
            return obj + "";
        }
        typeStr.split(",").forEach(function(value){
            class2type["["+"object"+ " "+value+"]"] = value.toLowerCase();
        });
        console.log(Object.prototype.toString.call( obj ));
        console.log(class2type);
        return typeof obj === "object" || typeof obj === "function"?
            class2type[ Object.prototype.toString.call( obj ) ] || "object" :  //怕获取不到类型,所以给以匹配不到的object
            typeof obj;
    }
    return util;
});
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页