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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:56:10+00:00 2026-05-25T15:56:10+00:00

I’ve read a lot of things about the module pattern. Ok It brings structure,

  • 0

I’ve read a lot of things about the module pattern.
Ok It brings structure, private method, etc…
But with the code below I can get the same behavior without using it.

function Human()
{
  // private properties
  var _name='';
  var _age=0;


  // private methods
  function created()
  {
    console.log("Human "+_name+" called");
  };

  // public
  this.setName = function(name){
    _name=name;
    created(); 
  };

}


var h1 = new Human();

h1.setName("John");

So, what are the real advantage of a module pattern finally ?

  • 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-25T15:56:11+00:00Added an answer on May 25, 2026 at 3:56 pm

    I think this example could help you to clarify the usefulness of the Module Pattern.

    Module Pattern

    The module pattern is widely used because it provides structure and helps organize
    your code as it grows. Unlike other languages, JavaScript doesn’t have special syntax
    for packages, but the module pattern provides the tools to create self-contained decoupled
    pieces of code, which can be treated as black boxes of functionality and added,
    replaced, or removed according to the (ever-changing) requirements of the software
    you’re writing.

    The module pattern is a combination of several patterns, namely:

    • Namespaces
    • Immediate functions
    • Private and privileged members
    • Declaring dependencies

    The first step is setting up a namespace. Let’s use the namespace() function from earlier
    in this chapter and start an example utility module that provides useful array methods:

    MYAPP.namespace('MYAPP.utilities.array');
    

    The next step is defining the module. The pattern uses an immediate function that will
    provide private scope if privacy is needed. The immediate function returns an object – the actual module with its public interface, which will be available to the consumers of
    the module:

     MYAPP.utilities.array = (function () {
        return {
        // todo...
        };
     }());
    

    Next, let’s add some methods to the public interface:

    MYAPP.utilities.array = (function () {
       return {
          inArray: function (needle, haystack) {
             // ...
          },
          isArray: function (a) {
             // ...
          }
       };
    }());
    

    Using the private scope provided by the immediate function, you can declare some
    private properties and methods as needed. Right at the top of the immediate function
    will also be the place to declare any dependencies your module might have. Following
    the variable declarations, you can optionally place any one-off initialization code that
    helps set up the module. The final result is an object returned by the immediate function
    that contains the public API of your module:

    MYAPP.namespace('MYAPP.utilities.array');
    MYAPP.utilities.array = (function () {
       // dependencies
       var uobj = MYAPP.utilities.object,
           ulang = MYAPP.utilities.lang,
           // private properties
           array_string = "[object Array]",
           ops = Object.prototype.toString;
           // private methods
           // ...
           // end var
       // optionally one-time init procedures
       // ...
       // public API
       return {
          inArray: function (needle, haystack) {
             for (var i = 0, max = haystack.length; i < max; i += 1) {
                if (haystack[i] === needle) {
                   return true;
                }
             }
          },
          isArray: function (a) {
             return ops.call(a) === array_string;
          }
          // ... more methods and properties
       };
    }());
    

    The module pattern is a widely used and highly recommended way to organize your
    code, especially as it grows.

    “JavaScript Patterns, by Stoyan Stefanov
    (O’Reilly). Copyright 2010 Yahoo!, Inc., 9780596806750

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string

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.