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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:51:00+00:00 2026-05-23T16:51:00+00:00

I have a JavaScript question. Let me quote from the book ‘Learning jQuery’, page

  • 0

I have a JavaScript question. Let me quote from the book ‘Learning jQuery’, page 389-390. The question concerns Closures.

I don’t understand why “the second call to outerFn() is not resetting the value of outerVar, but [is], rather, creating a new instance of outerVar, bound to the scope of the second function call.

What is it about JavaScript that would make this so? What do I need to know about JavaScript so that I could infer this for myself?

The rest of the email is the quotation from the book.

function outerFn() {
  var outerVar = 0;
  function innerFn() {
    outerVar++;
    $('#example-7').print('outerVar = ' + outerVar);
  }
  return innerFn;
}
var fnRef = outerFn();
fnRef();
fnRef();
var fnRef2 = outerFn();
fnRef2();
fnRef2();

Now our function calls have more interesting behavior:
outerVar = 1
outerVar = 2
outerVar = 1
outerVar = 2
We get a mix of the two earlier effects. The calls to innerFn() through each
reference increment outerVar independently. Note that the second call to
outerFn() is not resetting the value of outerVar, but rather creating a new
instance of outerVar, bound to the scope of the second function call. The
upshot of this is that after the above calls, another call to fnRef() will
print the value 3, and a subsequent call to fnRef2() will also print 3. The
two counters are completely separate.

  • 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-23T16:51:01+00:00Added an answer on May 23, 2026 at 4:51 pm

    You might be over thinking it.

    As always, every time a call to outerFn is made, it will create a new outerVar variable for that particular invocation of the function — it’s part of it’s local scope. Thus, any functions that are within the scope of the particular call to outerFn that are returned (or passed along somehow) will have continue to have access to that scope — a closure.

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

Sidebar

Related Questions

I have a question on jQuery $(document).ready Let's say we have a HTML page
Let me give my question some context. I have a JavaScript widget. The widget
Beginner rails/javascript question: Let's say that I have a simple Circle model in a
I have a question on javascript window management. My application opens an iframe which
I have a simple question concerning Javascript. I am trying to print in a
I'm trying to use the replace function in JavaScript and have a question. strNewDdlVolCannRegion
I am making a webapp. I have a fairly basic question about javascript performance.
I have a question regarding the onkeypress event on JavaScript. Is it possible to
I have a question similar to the one here: Event handlers inside a Javascript
Just finished reading Crockford's JavaScript: The Good Parts and I have a question concerning

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.