Tuesday, December 6, 2011

Undefined… uhhh… defined

Yes, we all know it, Javascript has some serious flaws. One of the more disturbing one being that undefined can be defined:

// This is way legal, but way weird:
undefined = “defined”;

Of course, it’s easy enough to avoid this, just don’t f-ing do it. But when writing scripts we are often playing in the same sandbox as the “big bad bullying, just straight up evil kids that has nothing better to do than going around defining undefined” are playing in. That’s why it’s very common to see scripts testing if the type of a value is “undefined” rather than to test if a value is equal to undefined:

// Test...
if (typeof foo === “undefined”) doStuff();
// ... rather than ...
if (foo === undefined) doStuff();

When I was a kid I alway wanted some sort of invisible shield to protected me from the big bad kids. Well, actually what I really, really wanted was some spinach to make me strong as Popey in order to beat the living crap out of the evil kids…

popeye

On the other hand, I guess a shield would do, I mean, even a visible shield would do. Can we please have a shield for our undefined??? Yes we can! And of course it comes in the form of a self invoking function as always when we want to shield our scripts.

(function(undefined) {
    if (something === undefined) doStuff();
}());

As you see here, we have an argument “undefined” in the function, however the function is self invoked without any argument thus the undefined argument has the value undefined. Inside this shield it’s perfectly safe to test for equality of undefined. This can be combined with other arguments as long as the undefined argument is the last one and the function is invoked with one argument less than the length of the arguments list:

(function($, global, undefined) {
    $(document).ready(function() {
        if (something === undefined) doStuff();
    });
}(jQuery, this));