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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:29:56+00:00 2026-05-31T20:29:56+00:00

just a bit confused by this code var counter = function() { //… var

  • 0

just a bit confused by this code

var counter = function() {
    //...
    var count = 0;
    return function () {
        return count = count + 1;
    } 
}

var nextValue = counter();  
console.log(nextValue());
nextValue.count = 7;
console.log(nextValue());
console.log(nextValue.count);
console.log(nextValue());

Output is

1
2
7
3

It’s counter intuitive. There are two representations of count. One on the outerfunction nextValue and one that only the inner anonymous function can see.

Correct, or are my missing something?

  • 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-31T20:29:57+00:00Added an answer on May 31, 2026 at 8:29 pm

    The expression nextValue.count does not refer to the local variable “count” declared inside the function. It is not possible, in fact, to create a reference to a variable local to a function from code outside the function. What you’re referencing there is simply a property of the function object.

    So, yes, the “count” variable that the returned function has access to is effectively completely private to that function, and it is persisted in the closure formed by the call to the “counter” function.

    If you did want that to work, you could do this:

    function counter() {
      function actual() {
        return actual.count = actual.count + 1;
      }
      actual.count = 0;
      return actual;
    }
    

    edit — (fixed bogus code) the name “actual” inside gives the returned function safe access to the function object itself; originally I typed “this” there, and that would not work unless the external code set it up explicitly.

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

Sidebar

Related Questions

This may seem silly, but I'm a bit confused about the following code: public
I am a bit confused why this code compiles. I leave out the necessary
I've just started playing with PowerMock and EasyMock and I'm a bit confused about
Sorry. I'm just bitten (or bit. Go figure with this left-to-right language :-) I've
Bit confused with this one. Basically, I have a column in a table, and
I'm a little bit confused now after I've seen a code snippet for iPhone
This might be quite simple, but I must say I'm a bit confused on
I came across this XPath in one of my studying code: count($recprv//*[local-name()='provider_email' or local-name()='provider_fax'
I'm just starting to learn web dev, but got myself a bit confused when
I've just read Ada Programming but I'm a bit confused about how to use

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.