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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:48:27+00:00 2026-06-11T10:48:27+00:00

Here is my self-executing function: var incrementInt = (function() { var manyOut = 10;

  • 0

Here is my self-executing function:

var incrementInt = (function() {
    var manyOut = 10;

    if(incInt) {
        incInt+=manyOut;
    } else {
        var incInt = 0;
    }

    return {
        s: incInt,
        m: manyOut
    };
})();

I have privatized the variables incInt and manyOut, and tied the variable incrementInt to the returned object.

My goal is to create a function that returns an object where one property is an integer that increments every time that it is called. I would like the incrementing variable to have the most narrow scope possible.

My problem with the solution I have above is that the variable incInt is re-initialized every time the function is called. Due to its scope within the function, the variable is destroyed automatically.

  • 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-11T10:48:29+00:00Added an answer on June 11, 2026 at 10:48 am

    You’ll need to use methods to add and get the variable incInt.

    var incrementInt = (function() {
        var manyOut = 10,
            incInt = 0;
    
        return {
            add: function() {
                return incInt += manyOut;
            },
            getInt: function() {
                return incInt;
            }
        };
    
    })();
    
    
    var a = incrementInt;
    
    
    a.add();
    a.add();
    
    a.getInt(); // 20​​​​​​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to return the value of self calling function into variable that
I'm writing a jQuery extension contained in a self executing function: (function($) { //
I always use the following self executing function in order to avoid exposing my
When I run this code: function foo() { console.log(foo); var self = this; this.a
Here is a self executing anonymous method. It seems to be good practice to
It is said here: http://briancrescimanno.com/2009/09/24/how-self-executing-anonymous-functions-work/ Take a look at the source code of jQuery
Here is what I want to do: class demo(object): def a(self): pass def b(self,
Here is my code: class MyTestCase(Base): def setUp(self): #some code here def test_B(self): #some
Here's my code: UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(rightButtonPressed)]; [self.navigationItem setLeftBarButtonItem:leftButton]; [leftButton
Here is the UIPanGesture subclass: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@touches began); self

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.