Loading

比typeof运算符更准确的类型判断函数

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

type({}); // "object"
type([]); // "array"
type(5); // "number"
type(null); // "null"
type(); // "undefined"
type(/abcd/); // "regex"
type(new Date()); // "date"

posted @ 2017-08-27 15:16  lewiskycc  阅读(118)  评论(0编辑  收藏  举报