16

I was looking at the past versions of jQuery's code, and it seems that in every version they have this line of code somewhere inside:

window.undefined = window.undefined;

I am not able to see why is this important and more importantly, what does this do. This seems like assigning undefined to undefined which makes no sense to me.

However this seems important and I am curious about it.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • possible duplicate of [Passed in undefined argument in jQuery core source code](http://stackoverflow.com/a/5395329/901048) – Blazemonger Dec 17 '13 at 19:58
  • 4
    @Blazemonger no, I don't think so. – Matt Ball Dec 17 '13 at 19:59
  • 2
    I think they are just making sure that the undefined property will exist. If it isn't, it's created – nice ass Dec 17 '13 at 20:01
  • It was removed in this version: http://code.jquery.com/jquery-1.1.4.js – Kevin B Dec 17 '13 at 20:03
  • 4
    @t0mppa, nope, it still assigns it to itself. `window.undefined` will contain exactly what it contained before the assignment (unless it does not exist in the first place). – Frédéric Hamidi Dec 17 '13 at 20:03
  • Seems like a safety check for `undefined` that someone didn't think all the way through but, like most code, existed for a long time before it was properly refactored/removed. – Evan Davis Dec 17 '13 at 20:05
  • The only thing it does seems to be assigning itself to itself, and this line of code was kept from the start till 1.1.4, which is quite long (1-2 years). – Derek 朕會功夫 Dec 17 '13 at 20:05
  • Who says that everything in jQuery has a logically objective reason? Maybe it is there for "historical" reasons. – davidkonrad Dec 17 '13 at 20:08
  • 2
    Since the bug tracker doesn't go back that far, you'll likely have to either dig through https://code.google.com/p/jqueryjs/source/list?num=250&start=2692 or ask one of the core developers of jquery during that time period. – Kevin B Dec 17 '13 at 20:09

2 Answers2

10

window.undefined did not always exist, so code like if (foo === undefined) would throw a ReferenceError in older browsers, because undefined is not defined (i.e. it's not declared). This code just makes sure that undefined is defined (as undefined).

It is sort of unnecessary, though, because there are so many ways to get the value undefined without the eponymous global property. For example you could write if (foo === void 0) or even something like if (foo === [][0]).

Later versions seem to assume that window.undefined exists in all browsers supported. The most recent versions use a pattern like this:

(function( window, undefined ) {
    ...
})( window );

Notice that no value is passed in for undefined, so it's guaranteed to be declared as a local variable, and have a value of undefined.

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • The global `undefined` variable was specified back in EcmaScript 3 (§15.1.1.3). Did browsers just not implement this, or have there been earlier versions that did not specify this? Which older browsers exactly do not have a global `undefined` variable? – Bergi Dec 17 '13 at 20:31
  • 1
    @Bergi IIRC it was an old IE thing. I'm trying to find some evidence of this now, but having trouble digging up info on those old browsers. I do remember running into this years ago, though. – Dagg Nabbit Dec 17 '13 at 20:35
  • @Bergi I just checked version 2 and it doesn't seem to be specified there. IE was pretty far behind the curve in fully adopting ES3, so I suspect it was them. – Dagg Nabbit Dec 17 '13 at 20:47
  • Citation required. What browsers do you speak of? – Benjamin Gruenbaum Dec 17 '13 at 21:47
  • @BenjaminGruenbaum a bit of googling indicates that `undefined` didn't exist in IE 5, which makes sense given that IE5 launched 9 months before ES3 was published, and ES2 didn't specify a global `undefined` property. Since MS didn't really do much to get IE updates in front of users back then, people could have IE5 sitting on machines for years, and the oldest jQ versions probably wanted to support those users. – Dagg Nabbit Dec 17 '13 at 21:59
  • @BenjaminGruenbaum I remember visiting a school's computer lab in 2003 or so and half of the computers had IE6, maybe a third had IE5.5, and the rest still had IE5. These machines didn't show any sign that they needed to be updated, the students who used them didn't have the administrative privileges to do it, and the administration didn't know any better. – Dagg Nabbit Dec 17 '13 at 22:09
1

Using this, jQuery defines a global variable with the real undefined value. having a global variable like myvar means you have this variable in your window scope (like window.mywar), if you run this code in the window scope:

var myvar = "whatever";

javascript defines your variable as if you have run this:

window.myvar = "whatever";

Let's assume we don't have a global variable like myundefined which means window.myundefined does not exist and window.myundefined has no value, it means window.myundefined is really undefined, then you can create a global variable named undefined in your window scope like this:

window.undefined = window.myundefined

and this line has the exact same result as the jQuery code that you have mentioned above. In other words jQuery could have done the same with this code:

window.undefined = window.myundefined

or

window.undefined = window.whatever_you_want_with_no_value

these both define a global variable with no value.

Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35