0

Is there any difference between creating an 'anonymous' namespace and then creating one with a throwaway name, or are these both the same for all intents and purposes?

(function(){
    console.log('namespace1');
}());
 
(function _(){
    console.log("namespace2");
})()

Note: duplicate mentions what an immediately invoked function is. I'm asking about if there's a difference between immediately invoked function and function-expression.

David542
  • 104,438
  • 178
  • 489
  • 842
  • the braces forces the expressions to evaluate. the order does not matter. for the reader of the code, i prefer to use `void` along with the expression without (outer) braces. – Nina Scholz Oct 18 '21 at 20:20
  • 2
    Depends on what you mean by "namespace". Those are just IIFEs, which simply create scope for variables. A namespace, to me anyway, is something different; it's a name under which other named things get placed. – Heretic Monkey Oct 18 '21 at 20:21
  • 1
    If you want to refer to the executing function, a named function expression allows it; an anonymous function does not. So, I guess, no they are not the same for *all* intents and purposes. – Heretic Monkey Oct 18 '21 at 20:37
  • 1
    [Why are anonymous function expressions and named function expressions initialized so differently?](https://stackoverflow.com/q/15129504/215552) may be edifying. – Heretic Monkey Oct 18 '21 at 20:43

0 Answers0