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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:58:21+00:00 2026-05-25T11:58:21+00:00

I have been reading about closures and javascript, and I thought I got it,

  • 0

I have been reading about closures and javascript, and I thought I got it, till I tried this:

var Object5 = function (param) {
    var x = 0;

    var squareBrace = function () {
        return '[' + param + x + ']';
    };

    this.toString = function () {
        x = x + 1;
        return squareBrace();
    };
};

Then I ran this code:

var counter = new Object5("Counter: ");
print("Has x:" + ('x' in counter));
print("Has f:" + ('f' in counter)); 
print("Can access x:" + (!!counter.x));   
print("Can Invoke f:" + (!!counter.f));   
print(counter.toString());
print(counter.toString());
print(counter.toString());
print(counter.toString());
print(counter.toString());
print(counter.toString());

And that is what I got:

Has x:false
Has f:false
Can access x:false
Can Invoke f:false
[Counter: 1]
[Counter: 2]
[Counter: 3]
[Counter: 4]
[Counter: 5]
[Counter: 6]

I thought I would get a ‘TypeError’ because ‘x’ and ‘f’ would be ‘undefined’, but then I got it working. I thought closures were for enable this behavior, ‘x’ and ‘y’ are ‘private’ and without a closure those member would be forgotten.

Apparently I got it all wrong, or I am missing something important here.

Could please somebody tell me what are closures for then and why this work?

Thanks.

  • 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-25T11:58:22+00:00Added an answer on May 25, 2026 at 11:58 am

    A closure is a scoping technique. It’s a way of pulling a parameter defined in one scope into another scope. When you do

    var x = 1;
    
    var f = function(){
       console.log(x); // you've just 'closed in' the variable x.
    };
    
    f();
    

    x will be available in the function even though its not declared in the function.

    In your specific instance, 2 variables are closed-in: param and x. They are bound to the scope of the functions you defined. When you execute toString, your are incrementing the closed in x. When toString executes squareBrace, that method uses both x and param. So you have 2 closures around the variable x, one for each method (its also in the scope of the object itself)

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

Sidebar

Related Questions

i have been reading about a function that can overwrite its return address. void
Recently I have been reading about lotto wheeling and combination generating. I thought I'd
I have been reading about this but I have to do some thing. I
I have been reading about the function of __init__.py file. It is said that
I have been reading about creating an RPM for Python 2.6.4. In this page:
I have been reading about this Sony vs GeoHot case. I however don't understand
I have been reading about how to build plug-ins and this MIME type keeps
I have been reading about the differences between Table Variables and Temp Tables and
I have been reading about collision detection in games on stackoverflow and other sites.
I have been reading about the continuation passing style programming technique (C# 3.0 implementation).

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.