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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:05:07+00:00 2026-06-02T03:05:07+00:00

I get the basics of the Module Pattern and its use of a closure

  • 0

I get the basics of the Module Pattern and its use of a closure to allow for private members, BUT I can’t quite swallow why the below code does what it does:

var Calculator = function() {
    var priv = 0;

    return {
        changePriv: function() { priv++;},
        printPriv: function() { console.log(priv);}
    }
}

var myCalc = Calculator();
myCalc.printPriv();
myCalc.changePriv();
myCalc.printPriv();

var myOtherCalc = Calculator();
myCalc.printPriv();

The console output is

0
1
1

So purposefully omitting the new keyword here, the first call sets myCalc to a Calculator object. It starts out with a priv value of 0, has that incremented, and then prints out its new priv value of 1.

But a) WHY does the next call to Calculator() end up returning a reference to the SAME object (as evidenced by the second ‘1’)? I know I can use new here and avoid that, but don’t get why I have to. Isn’t this function using object literal syntax to essentially create a new object and then return it?
b) Since it does seem to be using the same function stack space (is that even the right way to think of it in JS?), why doesn’t it zero out the priv variable in the process before returning the reference to the same object?

EDIT: Corrected sloppy/stupid mistake (thanks scessor), which DOES now output a new/distinct calculator object even without use of the new keyword. So that clears up a) and b). My resulting question would have been “Does it matter whether I use new or not in the invocation of a module-pattern constructor. The answer is, I guess it doesn’t matter(?). (Joseph: see http://jsfiddle.net/MvMvy/5/ …the instanceof operator simply doesn’t work with the module pattern either way.)

  • 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-02T03:05:08+00:00Added an answer on June 2, 2026 at 3:05 am

    You don’t output the other calculator myOtherCalc: if you want to compare them, replace the third myCalc.printPriv(); with:

    myOtherCalc.printPriv();
    

    Then the output is:

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

Sidebar

Related Questions

I'm trying to get the nodeunit module working within a coffeescript project but can't
This is a homework question, I got the basics down, but I can't seem
Wow, this seems so basic, but I can't get it to work. All I
I am trying to use the JavaScript Module Pattern and I run into a
I am trying to get a basic idea of how the os.system module can
I am following this titorial on roseindia to get basics of Hibernate : http://roseindia.net/hibernate/hibernate-update.shtml
I think I get the basics of multi-threading with Java. If I'm not mistaken,
I'm trying to get the basics of an Android app that sends and receives
I am new to sessions, and think I get the basics of them, they
I am learning basics of EJB 3.0. I have managed to get a sample

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.