0

why a named 'Self-Executing Anonymous Function' not pollute the global scope?

chrome 75


//below is named 'Self-Executing Anonymous Function'
(function funcName(params) {

})()

// below is unnamed 'Self-Executing Anonymous Function'
(function (params) {

})()

As shown,an unnamed 'Self-Executing Anonymous Function' will not pollute the global because it has no name. but if the function has a name? Which has been tested,the named 'Self-Executing Anonymous Function' will not pollute the global either.

As is knowledged to all, the only way to seperate the scope is to define a function,but what happened now? the parentheses can do this? Or maybe this is just a syntax-sugar specially defined?

Wilkin Wendy
  • 223
  • 2
  • 15
  • 1
    Because it's not a function *declaration* but a function *expression*. You don't get a global entry for `someName` when you do `fn = function someName() {}` – VLAZ Jun 25 '19 at 14:51
  • There must be an existing QA for that... – Denys Séguret Jun 25 '19 at 14:51
  • @DenysSéguret it's a mix of a few. I'm hunting them down – VLAZ Jun 25 '19 at 14:52
  • 1
    [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) [What is the (function() { } )() construct in JavaScript?](https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript/8228308) [What is the difference between a function expression vs declaration in JavaScript?](https://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip) – VLAZ Jun 25 '19 at 14:53

0 Answers0