目前类型判断有三种方法:typeof 、instanceof 、Object.prototype.toString.call
typeof:只能判断基础类型和函数类型
typeof 2 // number
typeof true // boolen
typeof 'str' // string
typeof function(){} // function
typeof undefined // undefinedtypeof null // object
typeof {} // object
typeof [] // object
instanceof:只能判断引用类型
[] instanceof Array //true
{} instanceof Object //true
function(){} instanceof Function //true666 instanceof Number //false
'str' instanceof String //false
true instanceof Blooen //false
Object.prototype.toString.call:可以判断各种类型,就是结果比较不友好
Object.toString.call(2) // [object Number]
Object.toString.call('str') // [object String]
Object.toString.call(true) // [object Booleen]
Object.toString.call(undefined) // [object Undefined]
Object.toString.call(null) // [object Null]Object.toString.call(function(){}) // [object Funtion]
Object.toString.call({}) // [object Object]
Object.toString.call([]) // [object Array]
根据上面的各种方法,进行封装
1.基本类型和函数类型就直接用typeof直接返回
2.遇到{},[],null的用Object.prototype.toString.call判断后进行截取再转小写
function getType(type) {if(typeof type === 'object') {retutn Object.prototypeof.toString.call(type).slice(8,-1).toLowerCase()} else {retutn typeof type}
}