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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:39:48+00:00 2026-06-17T16:39:48+00:00

Consider: var module = {}; (function(exports){ exports.notGlobalFunction = function() { console.log(‘I am not global’);

  • 0

Consider:

var module = {};

(function(exports){

  exports.notGlobalFunction = function() {
    console.log('I am not global');
  };

}(module));

function notGlobalFunction() {
  console.log('I am global');
}

notGlobalFunction(); // Outputs "I am global"
module.notGlobalFunction(); // Outputs "I am not global"

What’s going on here? I get that if you call notGlobalFunction(), it will just call the second function.

But what is var module = {} doing? And why is it called again inside the first function?

It says this is commonly known as a self-executing anonymous function, but what does that mean?

  • 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-17T16:39:49+00:00Added an answer on June 17, 2026 at 4:39 pm

    Immediately invoked functions are typically used to create a local function scope that is private and cannot be accessed from the outside world and can define its own local symbols without affecting the outside world. It’s often a good practice, but in this particular case, I don’t see that it creates any benefit other than a few more lines of code because it isn’t used for anything.

    This piece of code:

    (function(exports){
    
      exports.notGlobalFunction = function() {
        console.log('I am not global');
      };  
    
    }(module));
    

    Would be identical to a piece of code without the immediate invocation like this:

    module.notGlobalFunction = function() {
       console.log('I am not global');
    };  
    

    The one thing that is different is that in the first, an alias for modules called exports is created which is local to the immediately invoked function block. But, then nothing unique is done with the alias and the code could just as well have used modules directly.


    The variable modules is created to be a single global parent object that can then hold many other global variables as properties. This is often called a "namespace". This is generally a good design pattern because it minimizes the number of top-level global variables that might conflict with other pieces of code used in the same project/page.

    So rather than make multiple top level variables like this:

    var x, y, z;
    

    One could make a single top level variable like this:

    var modules = {};
    

    And, then attach all the other globals to it as properties:

    modules.x = 5;
    modules.y = 10;
    modules.z = 0;
    

    This way, while there are still multiple global variables, there is only one top-level global that might conflict with other pieces of code.


    Similarly, an immediately invoked function creates a local, private scope where variables can be created that are local to that scope and cannot interfere with other pieces of code:

    (function() {
        var x, y, z;
    
        // variables x, y and z are available to any code inside this immediately invoked function
        // and they act like global variables inside this function block and
        // there values will persist for the lifetime of the program
        // But, they are not truly global and will not interfere with any other global
        // variables and cannot be accessed by code outside this block.
        // They create both privacy and isolation, yet work just as well
    
    
    })();
    

    Passing an argument into the immediately invoked function is just a way to pass a value into the immediately invoked function’s scope that will have its own local symbol:

    (function(exports) {
        // Creates a local symbol in this function block called exports
        // that is assigned an initial value of module
    })(module);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider: var resturl = "http://example.com"; cj.getJSON( resturl + "&callback=?", function(data) { console.log(data); } );
Consider this code: var img = new Image(); img.onload = function() { console.log(this.width); };
Consider the code: var app = angular.module(app, [], function($routeProvider) { $routeProvider .when(/page1, { controller:
Consider the following var l = console.log.bind(console); l(-0); // 0 l(0); // 0 l(0
Consider this example var task =Task.Factory.StartNew(()=>Console.WriteLine(test)); task.ContinueWith(antecendent => { ExceptionProcessor.HandleError(task.Exception.Flatten()); }, TaskContinuationOptions.OnlyOnFaulted); In this
Consider the following Javascript code: var a = []; var f = function() {
Consider such loop: for(var it = 0; it < 2; it++) { setTimeout(function() {
Let's consider we have a module called mod which has a function func .
Consider the following jasmine spec: describe(something.act(), function() { it(calls some function of my module,
Consider: var ret = { valid: true, message: "" }; var prtime = $(".ptime").val();

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.