Function expression vs IIFE
In the first function, which is a function expression,
test.init(); is not working.
- var test = function(){
-
- console.log("variable inside test");
-
- // This is a function inside another function (closure)
- var init = function(){
- console.log("variable inside init");
- }
- // returning an instance of init method will expose it to the closure.
- return{
- init:init
- }
-
- }
- test().init(); // working
test.init(); // not working - WHY?
But in the following function, which is an IIFE
test.init(); is working.
- var test = (function(){
-
- console.log("variable inside test");
-
- // This is a function inside another function (closure)
- var init = function(){
- console.log("variable inside init");
- }
-
- return{
- init:init
- }
-
- })();
- test.init(); // working
test().init(); // not working - WHY?
Why
test.init(); is not working in the first example but is working in the second example??
Thanks