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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:29:51+00:00 2026-06-12T13:29:51+00:00

First, a pseudo code example: ;(function(foo){ foo.init = function(baz) { … } foo.other =

  • 0

First, a pseudo code example:

;(function(foo){

    foo.init = function(baz) { ... }

    foo.other = function() { ... }

    return foo;

}(window.FOO = window.FOO || {}));

Called like so:

FOO.init();

My question:

  • What is the technical name/description of: window.FOO = window.FOO || {}?

I understand what the code does… See below for my reason(s) for asking.


Reason for asking:

I’m calling the passed in global like so:

;(function(foo){
    ... foo vs. FOO, anyone else potentially confused? ...
}(window.FOO = window.FOO || {}));

… but I just don’t like calling that lowercase “foo“, considering that the global is called capitalized FOO… It just seems confusing.

If I knew the technical name of this technique, I could say:

;(function(technicalname){
    ... do something with technicalname, not to be confused with FOO ...
}(window.FOO = window.FOO || {}));

I’ve seen a recent (awesome) example where they called it “exports“:

;(function(exports){
    ...
}(window.Lib = window.Lib || {}));

I guess I’m just trying to standardize my coding conventions… I’d like to learn what the pros do and how they think (that’s why I’m asking here)!

  • 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-12T13:29:52+00:00Added an answer on June 12, 2026 at 1:29 pm

    Pattern

    (function (foo) {
        ...code...
        foo.bar = baz;
        ...more code...
    }(window.FOO = window.FOO || {});
    

    There is no formal name for the pattern you describe, because it’s three separate patterns combined. Each pattern goes by multiple names, but for this post I will use the following terminology:

    • closure
    • alias
    • namespace extension

    Closure

    The base of the entire pattern is the closure. It is simply a function that is used to scope variables and functions such that they don’t pollute the global namespace:

    No closure

    //these declare window.foo and window.bar respectively
    //as such, they pollute the global namespace
    var foo;
    function bar() {}
    

    Closure, in this case, an Immediately Invoked Functional Expression (IIFE)

    (function () {
        //these declare foo and bar within the function
        //but they are not accessible outside the function
        var foo;
        function bar() {}
    }());
    

    The advantage of keeping variables within a closure is that you won’t have to worry about someone overwriting the variables that you’re using. This is especially important for temporary variables such as i or j that are used often.

    Alias

    The second important part of this pattern is aliasing. Aliasing allows a variable to be defined and used within a closure without needing to worry about what global namespace it resides in.

    Without Aliasing

    (function () {
        ...
        foo = window.SomeFunction(bar, baz);
        ...
    }());
    

    With Aliasing

    (function (sf) { //local name
        ...
        foo = sf(bar, baz);
        ...
    }(window.SomeFunction)); //global namespace
    

    This is especially important as it means that the global namespace can be changed across a large JavaScript file by changing the name in a single location. This is A Good Thing™. Additionally, minifiers can shorten the internal alias to a single letter variable name such as a, making for significant byte savings on minification.

    Namespace Extension

    The namespace extension pattern relies on the coalescing behavior of the or operator (||). In many languages, && and || return either true or false, but in JavaScript, && returns the first falsey value (false, 0, '', null, undefined), and || returns the first truthy value (anything that’s not falsey). For both operators, if the respective type is not found, the last argument is returned. This makes the || operator a convenient way of defining a new namespace only if it doesn’t already exist.

    Without namespace extension

    if (typeof window.Foo === 'undefined') {
        window.foo = {};
    }
    

    With namespace extension

    window.foo = window.foo || {};
    

    This is useful because it allows a namespace to be extended with additional properties and methods without having to worry about which order the properties and methods were defined in.

    In this first example, FileA would need to be executed before FileB:

    FileA.js

    window.foo = {};
    window.foo.bar = 'baz';
    

    FileB.js

    window.foo.fizz = 'buzz';
    

    In this second example, File1 and File2 could be executed in any order:

    File1.js

    window.foo = window.foo || {};
    window.foo.bar = 'baz';
    

    File2.js

    window.foo = window.foo || {};
    window.foo.fizz = 'buzz';
    

    All together now

    Using each pattern together creates a very powerful modular script:

    //use foo internally so that you don't have to worry about
    //what the global namespace is called
    (function (foo) {
        //declare variables internally that you want to keep local to the script
        var i,
            len,
            internal,
            qux;
        //declare functions/properties on the alias when you want to expose them
        foo.bar = function () {...};
    //extend the global namespace so that existing extensions are persistent
    }(window.FOO = window.FOO || {}));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Depth first searching algorithm whose pseudo code is given below: DFS(Vertex v)
This code explains pseudo elements first-letter and first-line and it's working fine. <html> <head>
The following pseudo-code is from the first chapter of an online preview version of
Using Oracle 10gR2, I need to produce something like the following pseudo-example from data
first sorry if its a stupid question but im lost a bit. So when
I wrote this code of BFS in c++ using wikipedia's pseudocode. The function takes
The pseudo code of what I currently have is Cost + CostTax = CostActual
first off, this question is not related to a specific language - I use
This is more of a general programming question so the code examples I give
I managed to run the following code to insert into my table on first

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.