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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:40:16+00:00 2026-06-07T18:40:16+00:00

While watching Learning to Love JavaScript, I got hung up on why this 2nd

  • 0

While watching Learning to Love JavaScript, I got hung up on why this 2nd version wouldn’t work. It just returns ‘function’. Is a closure not allowed to have a variable directly assigned to it?

function getCtr() {
    var i = 0;
    return function() {
        console.log(++i);
    }
}
var ctr = getCtr();
ctr();
ctr();
ctr();
/* console
1
2
3
*/
/*--------------------------------------
 *   This doesn't work
 */
var ctr = function() {
    var i = 0;
    return function() {
        console.log(++i);
    }
}
ctr();
ctr();
ctr();
/*   console
 *   => [Function]
 */

Video Link http://youtu.be/seX7jYI96GE?t=11m12s

  • 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-07T18:40:18+00:00Added an answer on June 7, 2026 at 6:40 pm

    It prints that because the function does return a function.

    Try

    ctr()();
    

    The two forms of function declaration you used have almost exactly the same effect. Both simply create a function and bind it to a symbol. All you really changed in the second version is the name involved (“ctr” instead of “getCtr”).

    That is, if your test had been like the first setup:

    var actualCtr = ctr();
    
    actualCtr();
    actualCtr();
    actualCtr();
    

    you’ll see that it really is the same.

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

Sidebar

Related Questions

I got this flash application where you can click a link while watching a
This camera program showing horizontal preview while we are watching vertically like its was
I Built an IoC container long time ago while watching this show http://www.dnrtv.com/default.aspx?showNum=126 few
I'm watching this lecture: http://www.youtube.com/watch?v=Kq4FpMe6cRs // the speaker states that 'bar' is just some
I found the following table structures while I was watching ruby on rails tutorial.
I been using the jquery ui for quite a while now. This is the
While watching some videos on Channel 9 about the Reactive Extensions for .NET, someone
is there a way to prevent triggering of property getter code while watching variables
While I am watching the demo with the following link http://www.jetbrains.com/resharper/demos/presentation/overview/refactorings/Refactor_this_demo.htm I saw this
While watching lecture 1B of the Structure and Interpretation of Computer Programs , there's

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.