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

  • Home
  • SEARCH
  • 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 8426575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:33:58+00:00 2026-06-10T04:33:58+00:00

Wanting to get something straight here…so I have 2 questions The function below creates

  • 0

Wanting to get something straight here…so I have 2 questions

The function below creates a closure.

function Foo(message){
    var msg = message;

    return function Bar(){ 
        this.talk = function(){alert(msg); }
    }
};

Q: Which function is the closure, Foo or Bar?
I always thought the closure to be Foo, because it closes-over Bar once Bar is returned.

Next…

Below is the definition of an anonymous function:

()();

Q: Is the inner-function within this anonymous function also a closure?

(function(){ /* <-- Is this function also a closure? */ })();
  • 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-10T04:34:00+00:00Added an answer on June 10, 2026 at 4:34 am

    You need to use first principles here. Javascript uses lexical scoping. This means the scope of an execution context is determined by how the code is defined (lexical).

    I would say the definition of the function Bar is what causes the closure to be created, because msg is “closed-in” in the function.

    The actual creation of the closure happens at runtime (which is somewhat of a tautological statement, since nothing in a computer program happens until it is run), because in order to determine the value of msg, in Bar, when Bar is executed, the interpreter needs to know the value of the variable when Foo is executed, and so on up the chain.

    I’ll give two answers to your question. The pedantic answer is: neither function by itself is the closure. It’s the definition of variables within functions, combined with the execution context of functions when they are run, that is defines the closure. The common answer is: any function which closes over a variable is a closure (Bar in your case).

    Consider the problem everyone encounters when using Javascript.

    function A(x) {
       var y = x, fs = [];
    
       for (var i = 0; i < 3; i++) {
           fs.push(function(){
             console.log (i + " " + x);
           })
       }
    
       fs.forEach(function(g){g()})  
    }
    
    A('hi')
    

    Most people would say this would produce the output ‘hi 1’ followed by ‘hi 2’ followed by ‘hi 3’. However, it produces ‘hi 3’ 3 times. If just the definition of the function being added to the array, while using variables defined in the outer function, created the closure, how can this be?

    It’s because you need the execution context to define the closure, which doesn’t happen until runtime. At the execution of the functions in the array, i has the value 3. In the forEach statement, that’s the execution context, which is why the output always uses 3.

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

Sidebar

Related Questions

Straight to the point: I have this javascript: for(item=1;item<5;item++) { xmlhttp=new XMLHttpRequest(); xmlhttp.open(GET,'zzz.php', true);
I have been wanting to get a thumbnail of websites for quite some time
I have a gross looking function and am wanting to clean it up. All
I'm wanting to get the filepath ie /sites/default/files/myfile.pdf for my file uploads. I'm using
I'm wanting to get a WCF-over-TCP service working. I was having some problems with
My table named pictures has a column named pic and I'm wanting to get
I'm wanting to run AJAX to get some URL data, build an object from
I am wanting to pass something like the following to my view from my
I am wanting to test a function on one of my models that throws
Hello I have 2 tables that look like this, What I wanting to is

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.