underscore icon indicating copy to clipboard operation
underscore copied to clipboard

_.isUndefined is returning a ReferenceError on undefined objects

Open miwillhite opened this issue 15 years ago • 3 comments

Maybe I am misunderstanding the use of this method, but I thought it would work for objects that have not bee referenced yet.

I've recreated the issue here: http://jsfiddle.net/Gz4yK/ Using Chrome 12, ran into the issue on Underscore 1.1.6 and 1.1.7

When I make no prior reference to an object and pass it into isUndefined, I get a ReferenceError. If I set an object to undefined, then pass it to isUndefined it works as expected.

miwillhite avatar Jul 18 '11 21:07 miwillhite

Maybe I am misunderstanding the use of this method, but I thought it would work for objects that have not bee referenced yet.

lets think of a variable as box. In JavaScript this box can be filled with anything, text, numbers even more boxes( objects). _.isUndefined( foo) checks if the box with the name foo is empty. But in your case there is no box with the name foo. So Chrome has a problem here, you reference a box which does not exist. So it notifies you and sends a ReferenceError.

I agree, the name undefined is misleading, but thats just how its called.

mfG Hase

ps. meaning this ticket is invalid.

Kambfhase avatar Jul 18 '11 21:07 Kambfhase

@Kambfhase explained it nicely.

The use case usually is something like..

var hi;

if (something)
    hi = somethingElse;

_.isUndefined(hi);

Luckily there is a way to do what you want.

typeof hi === "undefined";

The typeof keyword doesn't throw reference errors.

aslilac avatar Jul 18 '11 21:07 aslilac

yep okay, that makes perfect sense. thanks!

miwillhite avatar Jul 18 '11 21:07 miwillhite