(function (root) {
var typeArr = [
'Null',
'Undefined',
'Object',
'Array',
'String',
'Number',
'Boolean',
'Function',
'RegExp',
'Element',
'NaN',
'Infinite'
],
type = function (o) {
var s = null,
type = "";
// handle null in old IE
if (o === null) {
return 'null';
}
// handle DOM elements
if (o && (o.nodeType === 1 || o.nodeType === 9)) {
return 'element';
}
s = Object.prototype.toString.call(o);
type = s.match(/\[object (.*?)\]/)[1].toLowerCase();
// handle NaN and Infinity
if (type === 'number') {
if (isNaN(o)) {
return 'nan';
}
if (!isFinite(o)) {
return 'infinity';
}
}
return type;
};
//use forEach fn
typeArr.forEach(function(val,index,arr){
type['is' + val] = function (o) {
return type(o) === val.toLowerCase();
};
});
//commonJS
if (typeof exports === "object" && exports) {
exports = type;
}else if (typeof define === "function" && define.amd) {
define(type);
}else {
root.type = type;
}
})(this);
我是煦涵
javascript 全类型检查type.js
最新推荐文章于 2024-08-09 07:08:58 发布