Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:04:46+00:00 2026-05-28T18:04:46+00:00

Tweetanium, a Titanium project widely refernced as a guide for best practices in Titanium:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:04:47+00:00Added an answer on May 28, 2026 at 6:04 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a Titanium app that I'm writing, I've structured a modular approach to where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.