function getMyType(value){
//判断数据是null的情况
if(value === null){
//直接转为null字符串,因为typeof可以识别undefined所以这里不再判断放在基础类型一起
return value + "";
}
//判断数据是引用类型
if(typeof value === "object"){
// [object Array]
let valueClass = Object.prototype.toString.call(value),
type = valueClass.split(" ")[1].split("");
// 拆分最后一个参数分为数组[ 'A', 'r', 'r', 'a', 'y', ']' ]
// console.log(valueClass);
// console.log(type);
// 将最后一个]pop掉
type.pop();
//重新将数组join成字符串返回
return type.join("").toLowerCase();
}else{
//判断是基本数据类型的情况和函数的情况
return typeof value;
}
}
let arr = [1,2,3]
console.log(getMyType(arr));
//array
以上就是代码的实现