For those of you who don't know JS, take the time to learn it! It’s actually quite fun to learn.
And the reason why the code will break it is simple, if not a bit weird:
JS has two types of null values:
Nullis an actual object, kinda like
NaN. You can call it and have no problems.
Undefined, on the other hand, is nothing (not even a keyword). I can even type
undefined=2and have no errors. Now, JS, with the
==operator, can't distinguish between the two. As in,
window.blahblah==nullreturns true even if
window.blahblahis not defined . But, JS has a wonderfully quirky operator known as the identity operator. This guy can tell the difference between
nulland undefined. Eg:
var poopy //gets a psuedo-default value "undefined" return poopy===null //Will return false. Poopy is not null, it is undefinedIt will only return true if you type poopy=null at the top. The reason for this is that
==compares objects, typecasting them if necessary (so
undefinedis typecasted to
===preserves type and compares.
Now, as I showed, using
window.blahblah==nullwon't tell you if the property is deliberately set to
nullor just not defined. Unfortunately, when developing frameworks which fit into larger applications, you don't have control over the rest of the code, but you still have to interact with it. Which means checking for undefined values becomes crucial. In that case, most people do this:
var undefined; //declare a variable which is not initialized return poopy===undefined // check if poopy is in the same state as the undefined variable, i.e. uninitializedThis would work even if I didn't call the variable undefined:
var bloopy return poopy===bloopyBut, JS programmers are obstinate fellows who always use the first method.
Now, if I type
undefined(i.e. uninitialized), and the program returns the exact opposite value that it should. Since checking for undefined values is quite essential in asynchronous scripting, this can make the scripts do unexpected things.
PS: If anyone here is working on a large framework, please, please type
var undefined=5somewhere in the code. And make sure your code doesn't use
undefinedfor checking for initialization. There you have it. Your code will be the only one that works when combined with other (sufficiently large) code.