1. typeof
undefine、string、function、boolean、number 均能被检测,其他一律返回 'object'
2. Object.prototype.constructor
- 不能检测基本类型(包含null),否则报错
- 用于检测引用类型
3. instanceof
不太靠谱,验证是 Array 还是 Object,还要多验证一次
4. Object.prototype.toString.call(varible)
基本上能够验证所有类型
5. Array.isArray
只能验证Array
应用——实现一个深拷贝
function deepCopy(obj){
if(typeof obj === "object" && obj !== null){
var result = obj.constructor == Array ? [] : {};
for(let i in obj){
result[i] = typeof obj[i] == "object" ? deepCopy(obj[i]) : obj[i];
}
} else {
var result = obj;
}
return result;
}
复制代码