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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:13:23+00:00 2026-06-02T18:13:23+00:00

I was reading this link http://addyosmani.com/largescalejavascript/#modpattern And saw the following example. var basketModule =

  • 0

I was reading this link http://addyosmani.com/largescalejavascript/#modpattern

And saw the following example.

var basketModule = (function() {
var basket = []; //private

return { //exposed to public
       addItem: function(values) {
            basket.push(values);
        },
        getItemCount: function() {
            return basket.length;
        },
        getTotal: function(){
            var q = this.getItemCount(),p=0;
            while(q--){
                p+= basket[q].price;
            }
        return p;
        }
      }
}());

basketModule.addItem({item:'bread',price:0.5});
basketModule.addItem({item:'butter',price:0.3});

console.log(basketModule.getItemCount());
console.log(basketModule.getTotal());

It stats that “The module pattern is a popular design that pattern that encapsulates ‘privacy’, state and organization using closures” How is this different from writing it like the below? Can’t privacy be simply enforced with function scope?

var basketModule = function() {
var basket = []; //private
       this.addItem = function(values) {
            basket.push(values);
        }
        this.getItemCount = function() {
            return basket.length;
        }
        this.getTotal = function(){
            var q = this.getItemCount(),p=0;
            while(q--){
                p+= basket[q].price;
            }
        return p;
        }

}

var basket = new basketModule();

basket.addItem({item:'bread',price:0.5});
basket.addItem({item:'butter',price:0.3});
  • 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-02T18:13:25+00:00Added an answer on June 2, 2026 at 6:13 pm

    In the first variant you create an object without the possibility to create new instances of it (it is an immediately instantiated function). The second example is a full contructor function, allowing for several instances. The encapsulation is the same in both examples, the basket Array is ‘private’ in both.

    Just for fun: best of both worlds could be:

    var basketModule = (function() {
       function Basket(){
            var basket = []; //private
            this.addItem = function(values) {
                basket.push(values);
            }
            this.getItemCount = function() {
                return basket.length;
            }
            this.getTotal = function(){
                var q = this.getItemCount(),p=0;
                while(q--){
                    p+= basket[q].price;
                }
            return p;
           }
         }
       return {
         basket: function(){return new Basket;}
       }
    }());
    //usage
    var basket1 = basketModule.basket(), 
        basket2 = basketModule.basket(),
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just reading this link: http://msdn.microsoft.com/en-us/library/aa833199.aspx It states: You cannot add users to roles in
I have this url for example: http://example.com?parameter=content When the user click in this link,
I was reading thourg this example (link) ... From the description, this seems paradox
After reading this question , i saw the answer by Naveen containing a link
Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for
I would like to use the WatchService API as mentioned in this link: http://download.oracle.com/javase/tutorial/essential/io/notification.html
I have been reading this link from Steven Sanderson about mobile web development http://www.asp.net/learn/whitepapers/add-mobile-pages-to-your-aspnet-web-forms-mvc-application
addressString = BOOKNO= [1] From= [ 2011-02-28 07:00:52] To= [2011-03-17 07:01:02] Link= [http://www.example.com] SINCE=
I am reading an article on extension of interface at following link. http://wiki.hsr.ch/APF/files/ExtensionInterface.pdf It
I was reading this link http://dec.bournemouth.ac.uk/staff/awatson/micro/articles/9907feat2.htm I could not understand this following statements from

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.