我正在尝试检查变量是否是这样的对象:
if(obj && typeof obj === Object) {
console.log('obj is an object and does not return null value');
}
我错过了什么?
答案 0 :(得分:5)
typeof返回类型的字符串表示形式,但是如果要检查null,则
if(typeof obj === 'object' && obj !== null) {
console.log('obj is an object and does not return null value');
}
答案 1 :(得分:0)
您的代码没问题,只需将Object替换为“object”字符串:)
答案 2 :(得分:0)
答案 3 :(得分:0)
'[object Object]' == Object.prototype.toString.call(obj)
答案 4 :(得分:0)
最好的方法是使用instanceOf
,best practice
if(obj instanceof Object) {
console.log('obj is an object and does not return null value');
}