If you just want the difference between all of the functions here is the difference.
First difference between funcA
, funcB
and funcX
, funcY
.
Whatever we have inside return
statement will be accessible by someModule
. So we can do both of these below
someModule.funcX();
someModule.funcY();
But we cannot do someModule.funcA() or someModule.funcB()
. To understand why that we can do this is to understand how ()
this behaves.
Whatever expression is put inside brackets is executed and result is returned there. So in this example we have put a function
inside these brackets, so an anonymous function will be created inside memory and it object will be returned here.
Now we have put one more pair of brackets after function declaration. So, what happens here is (returned function object)()
. So this function will be called immediately and an object containing two properties funcX
and funcY
whose value are function will be returned.
Now moving to second part that is inside the function. Here the difference between funcA
and funcB
is that funcA
is same as someModule
i.e. it is also an IIFE and depending upon the body of funcA
, it will be decided that if we are returning function object from inside the body of funcA
, then it will also be a function otherwise it will contain whatever we return from inside of body of funcA
.
Apart from that all other are simple functions.
Also, not related to what was asked. We can access funcA
and funcB
inside funcX
and funcY
, and even after containing function is executed then also we will still be able to access both of these function. For more details on this behavior look for closure in JavaScript. Happy learning JavaScript. :)