(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; });
js封装数据类型判断插件
最新推荐文章于 2024-04-20 22:10:42 发布