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 8229471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:48:21+00:00 2026-06-07T16:48:21+00:00

I refactored my JS code recently and stumbled upon this pattern: APP = (function()

  • 0

I refactored my JS code recently and stumbled upon this pattern:

APP = (function() {
  var x,y,z;
  function foo() {}
  function bar() {}
  return {x:x, y:y, z:z, foo:foo: bar:bar};
})();

The advantage to this is that it creates non-global variables with the functions having access to everything defined in APP. So APP.foo can access x, y, z, and bar without typing APP.bar(), APP.x, etc. Everything can also be accessed globally with APP.bar(), APP.x, etc. You can also nest them:

APP = (function() {
  var x,y,z;
  function foo() {}
  function bar() {}

  var WIDGETS = (function() {
    var a,b,c;
    function hello() {}
    function world() {}
    return {a:a, b:b, c:c, hello:hello, world:world};
  })();

  return {x:x, y:y, z:z, foo:foo: bar:bar, WIDGETS:WIDGETS};
})();

So WIDGETS would have access to variables in APP, but not visa versa (APP.WIDGETS.hello can use foo(), but APP.foo has to use WIDGETS.hello()).

I tried creating this pattern using ERB (I’m on Rails), but it turned out messy. So I’m thinking of writing a small source-to-source compiler for this – something like CoffeeScript (with the minimal difference/extended language philosophy of SASS) that just compiles a few functions to alternative javascript.

I just want a shorthand.

For example, this would compile to my second code block above:

//NAMESPACE is a magical function I compile down to the long version in the second code block
APP = NAMESPACE(function() {
  var x,y,z;
  function foo() {}
  function bar() {}

  var WIDGETS = NAMESPACE(function() {
    var a,b,c;
    function hello() {}
    function world() {}
    //**notice the missing return statement that I don't need to manage and map to my variables**
  });
  //**notice the missing return statement that I don't need to manage and map to my variables**
});

Simple and small – just so you don’t need to keep track of the variables. Would also like to separate the namespace like thus (so I can split it to multiple files):

APP = NAMESPACE(function() {
  var x,y,z;
  function foo() {}
  function bar() {}
  //**notice the missing return statement that I don't need to manage and map to my variables**
});
APP = NAMESPACE(function() {
  var WIDGETS = NAMESPACE(function() {
    var a,b,c;
    function hello() {}
    function world() {}
    //**notice the missing return statement that I don't need to manage and map to my variables**
  });
});

Any thoughts on how to do this? I’m not sure about everything, but I think if this existed, I’d like Javascript a lot more.

  • 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-06-07T16:48:24+00:00Added an answer on June 7, 2026 at 4:48 pm

    Things you might want to look into:

    • Narcissus: https://github.com/mozilla/narcissus/
    • Traceur: http://code.google.com/p/traceur-compiler/
    • Reflect.js: https://github.com/zaach/reflect.js
    • http://www.mascaraengine.com/doc/languagewalk
    • https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS

    There is also a proposal for a module system in EcmaScript 6 that could become available in the future: http://wiki.ecmascript.org/doku.php?id=harmony:modules

    Now, if the goal is just typing foo() instead of APP.foo() then I think creating a language superset of javascript is a bit of a stretch…

    If you use CoffeeScript exporting the variables is less verbose:

    APP = do ->
      [x,y,z] = []
      foo = ->
      bar = ->
    
      WIDGETS = do ->
        [a,b,c] = []
        hello = ->
        world = ->
        { a, b, c, hello, world }
    
      { x, y, z, foo, bar, WIDGETS }
    

    In practice you seldom export every variable, in fact you some of them to be “private”. You could also define the functions inside the object itself:

    APP = (function() {
      var x,y,z;
    
      var WIDGETS = (function() {
        var a,b,c;
        return {
          hello: function hello(){},
          world: function world(){}
        }
    
      })();
    
      return {
        foo: function foo(){},
        bar: function bar(){},
        widgets: WIDGETS
      }
    
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm refactoring some code that a friend wrote and recently stumbled across this function:
I recently refactored code like this ( MyClass to MyClassR ). #include <iostream> class
Recently I refactored the code of a 3rd party hash function from C++ to
I'm writing an android app that does a lot of stuff. I recently refactored
I'm currently working on a Perl web app LAMP style and recently stumbled upon
I recently refactored some ajax code to make it asynchronous. It was working perfectly
Is there anyway this code can be refactored? The only difference is the order
how would I refactor this code for ARC: - (UIGestureRecognizer *)createTapRecognizerWithSelector:(SEL)selector { return [[[UITapGestureRecognizer
Is there any way to refactor this code so that it can omit unnecessary
I recently refactored old code files from ABCFile.cpp/.h to AbcFile.cpp/.h to match my company's

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.