1

Modernizr starts with the following:

window.Modernizr = (function(window,document,undefined) { ...

...but why have window and document been passed as parameters? Is it something to do with differences between browsers? Or is it simply so the can't be redefined?

Cheetah
  • 13,785
  • 31
  • 106
  • 190
  • It makes it environment independent (more or less) and allows them to mock `window` and `document` for testing purposes. – Felix Kling Mar 03 '12 at 13:21
  • exact duplicate of [What advantages does using (function(window, document, undefined) { ... })(window, document) confer?](http://stackoverflow.com/questions/5020479/what-advantages-does-using-functionwindow-document-undefined-windo) – Bergi Aug 01 '13 at 00:39

2 Answers2

2

The parameters when calling that function are the following: (this, this.document) So it's guaranteed that window is the global object, document is the document object and undefined is undefined.

It also results in a small performance improvement; see Why does jQuery has a "window=this" at the very begining and say it would speed up references to window? for details

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

I believe it's for efficiently. window and document don't have to be fetched every time Modernizr needs it, but rather, a cached version of it.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308