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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:32:12+00:00 2026-05-28T05:32:12+00:00

I’m trying to get my head round the revealing module pattern in javascript. I’m

  • 0

I’m trying to get my head round the revealing module pattern in javascript. I’m puzzled by two things about the following code snippet.

        var Child = function () {
            var totalPoints = 100;
            addPoints = function (points) {
                totalPoints += points;
                return totalPoints;
            };
            getPoints = function () {
                return totalPoints;
            };
            return {
                points: totalPoints,
                addPoints: addPoints
            };
        };
        $(function () {
            var george = Child();
            console.log(george.points);
            console.log(george.addPoints(50));
            console.log(george.points);
        });
  1. The three values written to the console here are 100, 150, 100. That tells me that when I call “addPoints” with a value the totalPoints variable isn’t updated. If I examine the value of totalPoints within the addPoints function it has been incremented properly. What’s going on?

  2. If I use the console to examine window.addPoints or window.getPoints I can see that because I haven’t used “var” in front of the function declarations they’ve been added to the global scope. Isn’t that wrong? Most of the examples I’m looking at seem to do this.

Any pointers gratefully received.

  • 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-28T05:32:13+00:00Added an answer on May 28, 2026 at 5:32 am

    You’re passing a number here:

    return {
        points: totalPoints,
        addPoints: addPoints
    };
    

    This piece of code is no different from:

    return {
        points: 100,
        addPoints: addPoints
    };
    

    You’re passing the value; not a reference to totalPoints (the latter is not possible in JavaScript). So when totalPoints changes, the value in the object does not.


    Using a function

    The simplest way to get around this is to use a function to get the most up-to-date result (like getPoints which you already have). This JSFiddle gives a complete example:

    return {
        points: function(x) { return totalPoints; }, // always up-to-date
        addPoints: addPoints
    };
    

    The downside is that the caller must now ask for points as a function call:

    console.log(george.points());
    

    Using a getters and setters

    Another solution is using getters which would enable you to get an updated value with just george.totalPoints, though getters are not widely supported (yet). You could implement a getter like this:

    var obj = {};
    
    obj.points = addPoints;
    
    // add a "special" property to the object instead of normal notation
    Object.defineProperty(obj, "totalPoints", {
        get: function() { // function that's executed when you use `.totalPoints`
            return totalPoints;
        }
    });
    
    return obj;
    

    Secondly, dropping var makes the functions global which is correct but not advisable. You could make one var statement using commas, if that’s what you mean:

    var totalPoints = 100, // expands to three `var` statements, so no
        addPoints = ...,   // global variables
        getPoints = ...;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am reading a book about Javascript and jQuery and using one of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.