tags:

views:

195

answers:

4

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 how it's even possible to get object expected given all the checks that I'm doing to attempt to prevent something like that.

+4  A: 

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'.

Seb
But shouldn't the `b != null` catch that? (Note that I'm not using strict equality `===`.)
Domenic
Nope, because if b == 3, that's != null as well. Same with b being an array. You should make sure it's an *object*, not just != null...
Seb
`b` could be `undefined`.
Tomalak
alert(undefined == null); // true
Greg
If `b` is undefined, it short-circuits at `b != null`, since `undefined == null` (even though `undefined !== null`).
Domenic
Just as a side note: `typeof` is an operator and not a function. See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/typeof_Operator
Gumbo
+2  A: 

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

Tomalak
Shouldn't `typeof(b.processId) == "number"` prevent me from calling `b.processId` in the case of, say, `b = 3`?
Domenic
Asking for the `typeof(b.processId)` makes JavaScript evaluate `b.processId`. In case of `b` being the number 3, this would result in `"undefined"`. In case of `b` being `undefined` itself, it would result in an error.
Tomalak
I was under the impression that the whole point of `typeof` was to let you test if some property existed without actually dereferencing it. But, Firebug agrees that typeof(undefined.processId) gives an error, so I guess not >_<.
Domenic
Wait, you're working with a JScript engine? Then my bet is that it's one of JScript quirks. |var b=3; var c=b.processId;| is not an error. b===undefined wouldn't pass the null-check as you correctly said.
Nickolay
+2  A: 

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

var bPid = (typeof b != "undefined" && typeof b.processId == "number") ? b.processId : 0;
Gumbo
Would a (sloppy) `var bPid = (b ` do? After all, both `null` and `undefined` evaluate to `false`.
Tomalak
@Tomalak: No, you would get a *ReferenceError* if *b* does not exist.
Gumbo
I meant if `b` was a declared variable with an `undefined` value.
Tomalak
+2  A: 

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.

Kimmo Puputti