# 如何判断变量类型是对象
指广义上的对象这里,Array,RegExp,function。。。。等等
# 1. instanceof
{} instanceof Object // true
[] instanceof Object // true
1
2
2
# 2. typeof
function isObject(obj) {
const type = typeof obj
return obj && type === 'object' || type === 'function'
}
1
2
3
4
2
3
4
其余方案,编写isObject
函数稍许麻烦
- constructor
- Object.prototype.toString.call