Declaring functions inside an anonymous function

I am looking at code that seems to be declaring a function that needs to be called to run. This function is declared in an anonymous function. Does this not mean that the function will not be available for anything outside this block?

(function () { var _d = vjo.dsf.EventDispatcher; var _r = vjo.Registry; function $1(p0) { return function (event) { return this.onSubmit(p0, event); }; }; })(); 

Why would anyone do this? I am not sure about the purpose / relevance of $ in this code.

+6
source share
2 answers

"Does this not mean that the function will not be available for anything outside this block?"

Yes Yes.

"Why would anyone do this?"

Usually, since it only contains code for internal use, although in your example the function is never called.

"I have no goal / relevance" $ "in this code.

No relevance. Another valid variable character.

+9
source

The above example shows a general pattern for writing "modules" in javascript, although one that has an error. $ 1 is never called and is private, which means that it cannot exist either. However, in a valid example, additional code will be added that will call $ 1 and, possibly, other functions. Then, when this code has been included, it will all be evaluated, but the global namespace will not be contaminated with declarations.

+1
source

All Articles