[英]Javascript: typeof shows 'object', then null pointer thrown
Maybe someone can shed light on this. 也许有人可以阐明这一点。
I'm getting JS's version of an NPE on occasion, even though logging shows that typeof the offending variable is 'object'. 我有时会得到JS的NPE版本,即使日志记录显示有问题的变量的类型是'object'。
Here's the logging: 这是日志记录:
typeof myVar: object
ERROR in main._getFolderCount(): TypeError: myVar is null
console.error("ERROR in main._getFolderCount(): " + e);
Here's the code: 这是代码:
try{
console.log('typeof myVar: ' + typeof myVar);
if (typeof myVar !== 'undefined' && typeof myVar !== 'null'){
if (currentMsgsObj && currentMsgsObj.folderId == data[i].id && myVar.totalRows!=data[i].count) {
myVar.totalRows=data[i].count;
}
} else {
}
} catch (e) {
console.error("ERROR in main._getFolderCount(): " + e);
}
}
So you can see that the logging shows myVar is typeof 'object', and the code appears to pass the 'undefined/null' check, and then proceeds to blow up when a myVar member is accessed. 因此,您可以看到日志记录显示myVar是typeof'object',并且代码似乎通过了'undefined / null'检查,然后在访问myVar成员时继续崩溃。
该null
类型只具备返回object
使用时typeof
在其运营商。
In JavaScript, typeof null == 'object'
. 在JavaScript中,
typeof null == 'object'
。
You can check whether something is null
using ===
: 您可以使用
===
检查是否为null
:
if (blarg === null) ...
Using typeof blarg == 'null'
will never work. 使用
typeof blarg == 'null'
将永远无法工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.