0

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 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

5 Comments

But shouldn't the b != null catch that? (Note that I'm not using strict equality ===.)
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...
alert(undefined == null); // true
If b is undefined, it short-circuits at b != null, since undefined == null (even though undefined !== null).
Just as a side note: typeof is an operator and not a function. See developer.mozilla.org/en/Core_JavaScript_1.5_Reference/…
2

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

4 Comments

Shouldn't typeof(b.processId) == "number" prevent me from calling b.processId in the case of, say, b = 3?
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.
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 >_<.
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.
2

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

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

3 Comments

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.