function typeCheck(txt){
let typeString = Object.prototype.toString.call(txt).toLowerCase()
let typeArr = ['number','null','undefined','boolean','function','string','symbol','date','math','regexp']
let reallyType = 'object'
typeArr.forEach(item => {
if(typeString.indexOf(item) != -1){
reallyType = item
}
})
return reallyType
}
console.log(typeCheck('123456'))
使用正则更简便:
var type = function (o){
var s = Object.prototype.toString.call(o);
return s.match(/\[object (.*?)\]/)[1].toLowerCase();
};