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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:02:36+00:00 2026-05-23T17:02:36+00:00

I’m making headway on understanding closures, but I don’t understand how the following example

  • 0

I’m making headway on understanding closures, but I don’t understand how the following example even creates a closure. I’ll quote a passage from ‘Learning jQuery’, page 392, and what the author says about the example.

The author says that because “readyVar persists between calls to the function”, we can see a closure is involved. But, to me, readyVar persists between calls to innerFn simply because the anonymous function has not yet finished executing. Once the anonymous function finishes, there will be no references to readyVar and it will be garbage collected. So where is the closure? I’m under the impression that a closure involves a variable being referenceable in a scope outside of that scope in which the variable was defined. But I don’t see that here.

Is the author saying that anytime an inner function references a value defined in an outer function, a closure is created? That can’t be what he’s saying, can it?

The rest of this message is a quotation from the aforementioned book.

$(document).ready(function() {
  var readyVar = 0;
  function innerFn() {
    readyVar++;
    $('#example-9').print('readyVar = ' + readyVar);
  }
  innerFn();
  innerFn();
});

This looks like many of our earlier examples, except that in this case, the outer function is the callback passed to $(document).ready(). Since innerFn() is defined inside of it, and refers to readyVar which is in the scope of the callback function, innerFn() and its environment create a closure. We can see this by noting that the value of readyVar persists between calls to the function:
readyVar = 1
readyVar = 2

  • 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-23T17:02:36+00:00Added an answer on May 23, 2026 at 5:02 pm

    For the duration of the $(document).ready function, all it’s local variables are active and hold their values and any code in that function or any embedded functions can reference them. While one could describe this as a function closure, this is more just how local variables work in a function.

    If, inside the $(document).ready, there was an embedded function that stored a reference somewhere (like a click handler), then a lasting function closure would be established and the value of readyVar would last as long as any embedded references last. It’s essentially garbage collection. As long as something outside the function loop holds a reference to something inside the loop, the loop stays alive. At the point where the loop is done executing and nothing outside the loop holds any embedded function references to things inside the loop, the loop gets garbage collected and goes away.

    Since there are not embedded function references in what you have now, it will get garbage collected (e.g. destroyed and freed) when it finishes executing.

    Now, if you modified it to add a click handler like this, then you now have a lasting reference to a function inside the $(document).ready function with the click handler function. Since that lives on, even after the $(document).ready loop has finished executing, it will not be garbage collected and will live on as a function closure.

    $(document).ready(function() {
      var readyVar = 0;
      function innerFn() {
        readyVar++;
        $('#example-9').print('readyVar = ' + readyVar);
      }
      innerFn();
      innerFn();
      $("#content").click(function(){  // external reference to within this function
          alert(readyVar);
      });
    });
    

    I sense from your question that you’re confusing local variables with function closures. The two are related, but not the same thing. Local variables are always available for the lifetime of the function. The lifetime of the function is the time it’s executing except when a reference to a closure within it causes it to last longer than that.

    You might find this helpful: http://blog.morrisjohns.com/javascript_closures_for_dummies.html

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
i got an object with contents of html markup in it, for example: string

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.