js判断数据类型的封装函数以及判断函数。

var type = function (o){
  var s = Object.prototype.toString.call(o);
  return s.match(/[object (.*?)]/)[1].toLowerCase();
};

['Null',
 'Undefined',
 'Object',
 'Array',
 'String',
 'Number',
 'Boolean',
 'Function',
 'RegExp'
].forEach(function (t) {
  type['is' + t] = function (o) {
    return type(o) === t.toLowerCase();
  };
});    //给type函数对象封装各个判断的方法

type.isObject({}) // true
type.isNumber(NaN) // true
type.isRegExp(/abc/) // true
原文地址:https://www.cnblogs.com/sidianok/p/13224625.html