2

Tweetanium, a Titanium project widely refernced as a guide for best practices in Titanium: https://github.com/appcelerator-titans/tweetanium uses self-executing functions almost everywhere, even for function declarations like in this view:

(function() {
  //create the about view
  tt.ui.createAboutView = function(_args) {
    var aboutView = Ti.UI.createView($$.stretch);

        aboutView.add(Ti.UI.createLabel(tt.combine($$.headerText, {text:L('about')})));
        aboutView.add(Ti.UI.createView({
            top:$$.headerView.height,
            bottom:0,
            right:0,
            left:0,
            backgroundImage:'images/about_mock.png'
        }));

    return aboutView;
  };
})();

As far as I know, self-executing functions are great to keep the global namespace clean because functions have their own scope. But why on earth would you declare a function inside a self-executing function, what's the benefit of that?

Julian
  • 312
  • 1
  • 2
  • 14

2 Answers2

2

A lot of reasons...

keep your code clean, modulable, flexible, extensible etc... juste by adding or removing some part codes.

You can check librairies like Mootools ( core-builder ), jQuery ore others that provides sort of "bundles".

Those bundles very frequently can be used "as-is", without the others bundles ( except the very core one ). But they also provide functions and methods that can be used when they are loaded. Like here, this code the createAboutView to the application.

So you have (function() { //core code })(); (function() { //module one code with his own work/process functions })(); (function() { // module two with his own work/process function })();

Module one is independant of the module two. All he need is on the core ( and sometimes the required modules ) and in his own scope.

--- update

Module two needs some functions to work. But those functions are not needed by the core or the module one, just in the second module scope.

One another big reason is when you're declaring some function that depends on the current process. This topic show why you sometimes have to define functions ( and it can be recursive, function into function into function to keep scope and states ).

little exemple :

var a = [], j, k;
(function() {
    var i;
for(i = 5; i--;)
 a[i] = function() { alert(i); };


})();
for(k = 0, j = a.length; k < j; k++)
a[k]();

In this case, i is always -1, because when i call the function, i refer to the i on the self-executed function, which is now at '-1' because of the loop.

In those example below

var a = [], j, k;
(function() {
    var i;
for(i = 5; i--;)
      (function(i) { a[i] = function() { alert(i); }; })(i);


})();
for(k = 0, j = a.length; k < j; k++)
a[k]();

OR

 var a = [], j, k;
(function() {
    var i;
    var alerti = function(i) { 
        return function() {  alert(i); }; 
    };
for(i = 5; i--;)
      a[i] = alerti(i);


})();
for(k = 0, j = a.length; k < j; k++)
a[k]();

creating a function ( or an other self-executing function ) into a self-executing function allow me to keep a scope

Community
  • 1
  • 1
dievardump
  • 2,484
  • 17
  • 21
  • Good point. Libraries like that are depending on self-executing functions. However, this is a different case. I know for a fact that in this case the app would run fine without the self-executing function. So this doesn't quite yet qualify as an answer. – Julian Jan 29 '12 at 21:48
  • In this case, we don't know if in 3 months, they won't want to add some features to this "createAboutView". The self executing function is just here to keep the code into a scope " if one day... ". It surely is " rules " or " good practice " they decided to have for the development of this tool. – dievardump Jan 29 '12 at 21:57
  • Sort of answers my question. Thanks! – Julian Jan 29 '12 at 22:15
0

The benefits can be Readability, splitting the code to multiple functions, is almost always consider to be good practice.

Anyway in the example you included the function in the self executing function is stored as a variable in the global var tt.ui.createAboutView so there is a reason why it's declared...

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • Sorry but you're missing the point here. I'm aware that separating code is good practice. My question is: **Why declare a function inside a self-executing function?** – Julian Jan 29 '12 at 21:26
  • @Julian. I think I answered that question. read it again please. – gdoron Jan 29 '12 at 21:28
  • no you didn't. You say "there is a reason why it's declared" but you don't actually tell me what the reason is. – Julian Jan 29 '12 at 21:32
  • @Julian. Because it's used outside the self-executing function. – gdoron Jan 29 '12 at 21:34
  • That's incorrect. A function doesn't have to be declared inside a function to be used outside of a function. If the function is declared as part of a global variable you can call or define it anywhere... – Julian Jan 29 '12 at 21:40