4

I read so much about one of the uses of an iife is to help in namespace conflict resolution and how it makes it difficult to accidentally affect the global object & pollute the global scope. And how it leads to safer code.

My question is, all that already happens in a function that is not immediately invoked. Do we specifically need an iife to get those benefits? How does using IIFE & thus hiding and creating a separate environment, is any different from a regular function? In an iife, the variables are local (thus they do not pollute global scope) but so are they in non-iife functions.

Am I right in saying that iife do not help in variable name collision, but help in function name collision? Variables in even normal non-iife functions are locally scoped and not colliding with global variables, isn't it?

So, in a nutshell, when they talk about polluting the global scope specifically w.r.t. an iife, they are talking about not polluting the scope w.r.t. method names and not variable names ?

Kumar Manish
  • 1,166
  • 1
  • 16
  • 28
  • 1
    An IIFE does not leave any "traces" in the global namespace itself, as `function foo(){}` or `var foo = function(){}` would. And differentiating between function names and variable names is rather academic, when not polluting the global namespace is the objective - which one of those in your code (which I might for example import as a 3rd-party library) overwrite _my_ `foo` is irrelevant. – CBroe Jul 08 '18 at 08:33
  • Again, it is all about not leaving a trace w.r.t the *function* name pollution, not about the *variable* name pollution right? – Kumar Manish Jul 08 '18 at 08:42
  • Names are names. There aren't separate namespaces for variables and functions. – melpomene Jul 08 '18 at 08:44
  • @melpomene Names are names but only the *function* name would leave a trace in global scope, not a variable inside it. The function `function foo(){}` or `var foo = function(){}` would leave traces but any variable inside them would not. So there *is* a subtle difference. I repeat, it is more about the pollution done by the *function* names and less about variables names? – Kumar Manish Jul 08 '18 at 08:50
  • @KumarManish Yes. (See also the answer to the linked question.) – melpomene Jul 08 '18 at 08:51

1 Answers1

0

Am I right in saying that iife do not help in variable name collision, but help in function name collision?

Yes, an IIFE does not pollute the global namespace as both the function itaelf as well as the variables declared inside are not part of the global scope.

Variables in even normal non-iife functions are locally scoped and not colliding with global variables, isn't it?

As far as you declare them (which a lot of people forget), yes.

So, in a nutshell, when they talk about polluting the global scope specifically w.r.t. an iife, they are talking about not polluting the scope w.r.t. method names and not variable names ?

Actually both, as you need the function to hide the variables, and an IIFE to hide the function.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • `Actually both.` Any variable inside a function (if declared with var) will never pollute be it an iife or a regular function. It is not a benefit that should be attributed to an iife. – Kumar Manish Jul 08 '18 at 09:00