I recently saw code that looks like this:
(function (someGlobal) {
someGlobal.DoSomething();
})(someGlobal);
where someGlobal was a large framework class. It could be JQuery. What is the benefit of doing this? Is it a good practice? It seems like this code would be equivalent and less error prone because you don’t hide the global:
(function () {
someGlobal.DoSomething();
})();
You have a reliable reference in case the global one gets overwritten
It shortens the scope chain to access it
If you anticipate the global being overwritten, and your code relies on the overwritten value, then clearly you wouldn’t want to shadow it by a local variable in a function.
Aside from that, the local reference in a function shouldn’t be any more prone to error.