What are the differences between following two javascript code?

function, javascript

Solution

Two reasons I can think of:

1) Local variables are the first in the scope chain, so their access is faster than globals (with faster I mean insignificantly faster).

2) Inside the function, `window` and `document` are local variables, so their names can be minimified:

(function (w, d) {
//var userAgent = w.navigator.userAgent;

)(window, document);

Problem

In some Javascript code which uses immediate function, it has argument `window` or `document` like the following: ``` (function (window, document) { ... })(window, document); ``` However, `window` and `document` are global objects and can be directly accessed as follow: ``` (function () { var userAgent = window.navigator.userAgent; ... var el = document.getElementById(...) ... })(); ``` What are the differences between the above two codes. Which is better way and why?

Original source

Related problems