开发者

How is this "object expected" JavaScript error possible?

Apparently this line of code is triggering "Object expected":

var bPid = (b != null && typeof (b.processId) == "number") ? b.processId : 0;

Unfortunately I can't step through the code in the debugger since this is an intermittent error that shows up in a Windows SideShow gadget that I'm writing. But, I'd imagine someone should be able to tell me ho开发者_JAVA技巧w it's even possible to get object expected given all the checks that I'm doing to attempt to prevent something like that.


It seems b is not an object, so I'd alert(b) before that line to see if it's been assigned a value at all.

Even if it has a value assigned, it may not be an object, so you might as well ask for typeof(b) == 'object'.


You are calling b.processId without making sure that b is an object.


Your variable b probably doesn’t exist. Try this:

var bPid = (typeof b != "undefined" && typeof b.processId == "number") ? b.processId : 0;


The safest (and the shortest) way to check if the b variable is 'truthy' (in Douglas Crockford's terms) would be

var bPid = (b && typeof (b.processId) == "number") ? b.processId : 0;

unless you explicitly want to compare it to null (in which case you should compare with === that doesn't do type coersion).

And to be 'truthy' a varible is anything except false, null, undefined, NaN, the number zero, or an empty string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜