0

My js does this:

var MyClass ={
        a: 1,
        b: 2,
        init: function(message){ console.log("calling c to say "+message);};
 };

 MyClass.init("hello");

//all above code is in js file

I keep seeing the IIFE pattern all over, but I am afraid I don't see what benefit it gets me compared to above. I have a module MyClass and ability to call methods on it. Is there a downside to using this pattern ?

Ka Pr
  • 75
  • 1
  • 5
  • 1
    the downside is that it's a plain object and you don't have the encapsulation provided by a closure. – VLAZ Aug 12 '16 at 20:33
  • Could you please elaborate on that ? I understand what is happening in the below answer, but that has nothing to do with my module creation. The answer is just exeuting a function and some code. There is no module or enacapsulation being created. Are you saying in my object I can access MyClass.a and MyClass.b etc., but if I were to use an IIFE, would be able to control that ? If I had to rewrite this as an IIFE how would I do it ? thanks I am js newb – Ka Pr Aug 12 '16 at 21:18
  • Of course there is encapsulation - you cannot reach `x` because it's inside the closure. The answer doesn't create a module but it's because the intention is to show what your solution would be lacking, not because it's showing "how to make a module". Given that you claim you know that already, it'd be redundant to go over it again. – VLAZ Aug 12 '16 at 21:20
  • When I claim to be able to make a module, I a referring to the MyClass code above. If I had no need to have private variables like x, would my way of making "module" be sufficient ? If not could you please show me how to with IIFE ? I am going to have take a long look at it to understand – Ka Pr Aug 12 '16 at 21:36
  • I wrote [an answer](http://stackoverflow.com/a/38577016/3689450) about that a couple of weeks back, it's verbose but I think it should answer your questions. Feel free to check the links in there for additional information on the subject - most notably [closures](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1) (also shows up as related in this question) and the links I have on different takes on the module pattern. – VLAZ Aug 12 '16 at 21:50
  • thanks, I think I get it. IIFE affords a namespace and a module - by enabling private and public data/functions through closure. The pattern being c And my object literal just allows namespace, if I just wanted a namespace it would be enough. There is no danger of someone indavertantly updating MyClass.a, since there is no scope clash, if I modified my code to `var MyClass ={ var a: 1, var b: 2 }` – Ka Pr Aug 12 '16 at 23:29
  • Yes, if you just want to hold some functions/variables in one place, then an object is enough. You don't _have to_ use a closure all the time for every single thing. Above all, we should remember that a closure is merely a poor man's object. And an object is merely a poor man's closure. – VLAZ Aug 12 '16 at 23:39

1 Answers1

1

An IIFE is used to create a new function scope to avoid leaking variables into the global scope:

(function() {
    var x = 1;
    console.log(x); // 1
})();

console.log(x);     // undefined

This has basically nothing to do with calling a function stored in an object as in your example.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94