类型判断
2020-06-08 本文已影响0人
locky丶
检查当前元素的类型 (传入的type首字母可以小写)
export const checkType = (o, type) => {
let typeLowercase = type.toLowerCase()
return (Object.prototype.toString.call(o).slice(8, -1)).toLowerCase() === typeLowercase
}
console.log(checkType('foo', 'String'))
console.log(checkType(2,'Number'))
console.log(checkType([2,3,4],'Array'))
console.log(checkType(0, 'Boolean'))
console.log(checkType(undefined, 'undefined'))
console.log(checkType(null, 'null'))
console.log(checkType({'name':'foo'},'Object'))
console.log(checkType(Symbol('name'), 'Symbol'))
console.log(checkType(new Set('name'), 'Set'))
let map = new Map()
console.log(checkType(map.set('name','foo'), 'Map'))