在JavaScript里使用typeof判断数据类型,只能区分基本类型, 即:number、string、undefined、boolean、object。 对于null、array、function、object来说,使用typeof都会统一返回object字符串。 要想区分对象、数组、函数、单纯使用typeof是不行的。在JS中,可以通过Object.prototype.toString方法,判断某个对象之属于哪种内置类型。 分为null、string、boolean、number、undefined、array、function、object、date、math。
第一种:
function getType(obj) {
return Object.prototype.toString.call(obj).slice(8,-1);
}
字符串截取换成正则:
function getType(obj){
var s = Object.prototype.toString.call(obj);
return s.match(/\[object (.*?)\]/)[1].toLowerCase();
};