In the simple case, you showed that the anonymous function being executed immediately does not give any advantages.
However, you can declare variables or other functions within the scope of anonymous functions, and they will be effectively closed to your MyClass function. So there is a huge advantage, and even if you donβt need private variables now, you can later so that you can use the anonymous function anyway ...
Note also that placing a var statement inside an if is pointless, since the declaration (but not the destination) gets "raised" from the block.
source share