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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:44:40+00:00 2026-05-18T20:44:40+00:00

In the following code, why are both of the methods (increment and print) listed

  • 0

In the following code, why are both of the methods (increment and print) listed inside of return? Why can’t you just use return counter++? Also, what does it mean to return a console.log?

function create() {
  var counter = 0;
  return {
    increment: function() {
      counter++;
    },
    print: function() {
      console.log(counter);
    }
  }
}

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-18T20:44:41+00:00Added an answer on May 18, 2026 at 8:44 pm

    What this returns is more or less a Counter, if we rename the topmost function, it should make more sense.

    Well what does it do? Let’s add some comments

    function Counter() { // a function, nothing special here
      var counter = 0; // a variable that's local to the function Counter
      return { // return an object literal {}
        // which has a property named 'increment'
        increment: function() { // that's a function AND a closure
          counter++; // and therefore still has access to the variable 'counter' inside of Counter
        },
        print: function() { // another function
          console.log(counter); // this logs to the console in most web browser
                                // the console object was introduces by Firebug 
                                // and effort is made being to standardize it
        }
      }
    }
    

    Example usage:

    var a = Counter(); // you don't need the new keyword here sinc in this case
                       // there's no difference because the implicit return overwrites
                       // the normal constructor behavior of returning 'this'
    a.increment();
    a.print(); // 1
    var b = Counter();
    b.print(); // 0
    

    Note the variable counter inside the function is not accessible from the outside, therefore it’s readonly, an effect you can only achieve by using a closure.

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

Sidebar

Related Questions

I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
In the following code, both amp_swap() and star_swap() seems to be doing the same
In the following code, both the INPUT and TEXTAREA elements render wider than they
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
How can I ensure the following code is disposing of all objects in a
I am using the following code to call some save methods when my app
How does the following code work? public void SomeMethod() { StringBuilder sb = new
I have the following code in two different classes (both subclasses of UIView). In
In the following code, does ptrcall point... to 2 places on the heap with
I am using wxWidgets 2.9.2, when i use following code //ToolBar wxToolBar *mainTool =

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.