手写getType函数,获取详细数据类型
- 如输入 [1,2,3] 输出 arry; ‘你好’ 输出 string
- type of 不能判断复杂类型,instanceof只能判断true/false 不能输出类型
- 思路:Object.prototype.toString.call (x)
function getType(x:any): string{
const originType = Object.prototype.toSting.call(x) //'[object String]'
const spaceIndex = originType.indexOf(' ')//
const type = originType.slice(spaceIndex + 1, -1) //'String'
return type.toLowerCase() //string
}