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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:37:55+00:00 2026-06-15T16:37:55+00:00

I’ve seen the following three code blocks as examples of the JavaScript module pattern.

  • 0

I’ve seen the following three code blocks as examples of the JavaScript module pattern. What are the differences, and why would I choose one pattern over the other?

Pattern 1

function Person(firstName, lastName) {
    var firstName = firstName;
    var lastName = lastName;

    this.fullName = function () {
        return firstName + ' ' + lastName;
    };

    this.changeFirstName = function (name) {
        firstName = name;
    };
};

var jordan = new Person('Jordan', 'Parmer');

Pattern 2

function person (firstName, lastName) { 
    return {
        fullName: function () {
            return firstName + ' ' + lastName;
        },

        changeFirstName: function (name) {
            firstName = name;
        }
    };
};

var jordan = person('Jordan', 'Parmer');

Pattern 3

var person_factory = (function () {
    var firstName = '';
    var lastName = '';

    var module = function() {
        return {
            set_firstName: function (name) {
                               firstName = name;
                           },
            set_lastName: function (name) {
                              lastName = name;
                          },
            fullName: function () {
                          return firstName + ' ' + lastName;
                      }

        };
    };

    return module;
})();

var jordan = person_factory();

From what I can tell, the JavaScript community generally seems to side with pattern 3 being the best. How is it any different from the first two? It seems to me all three patterns can be used to encapsulate variables and functions.

NOTE: This post doesn’t actually answer the question, and I don’t consider it a duplicate.

  • 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-15T16:37:56+00:00Added an answer on June 15, 2026 at 4:37 pm

    I don’t consider them module patterns but more object instantiation patterns. Personally I wouldn’t recommend any of your examples. Mainly because I think reassigning function arguments for anything else but method overloading is not good. Lets circle back and look at the two ways you can create Objects in JavaScript:

    Protoypes and the new operator

    This is the most common way to create Objects in JavaScript. It closely relates to Pattern 1 but attaches the function to the object prototype instead of creating a new one every time:

    function Person(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    };
    
    Person.prototype.fullName = function () {
        return this.firstName + ' ' + this.lastName;
    };
    
    Person.prototype.changeFirstName = function (name) {
        this.firstName = name;
    };
    
    var jordan = new Person('Jordan', 'Parmer');
    
    jordan.changeFirstName('John');
    

    Object.create and factory function

    ECMAScript 5 introduced Object.create which allows a different way of instantiating Objects. Instead of using the new operator you use Object.create(obj) to set the Prototype.

    var Person =  {
        fullName : function () {
            return this.firstName + ' ' + this.lastName;
        },
    
        changeFirstName : function (name) {
            this.firstName = name;
        }
    }
    
    var jordan = Object.create(Person);
    jordan.firstName = 'Jordan';
    jordan.lastName = 'Parmer';
    
    jordan.changeFirstName('John');
    

    As you can see, you will have to assign your properties manually. This is why it makes sense to create a factory function that does the initial property assignment for you:

    function createPerson(firstName, lastName) {
        var instance = Object.create(Person);
        instance.firstName = firstName;
        instance.lastName = lastName;
        return instance;
    }
    
    var jordan = createPerson('Jordan', 'Parmer');
    

    As always with things like this I have to refer to Understanding JavaScript OOP which is one of the best articles on JavaScript object oriented programming.

    I also want to point out my own little library called UberProto that I created after researching inheritance mechanisms in JavaScript. It provides the Object.create semantics as a more convenient wrapper:

    var Person = Proto.extend({
        init : function(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        },
    
        fullName : function () {
            return this.firstName + ' ' + this.lastName;
        },
    
        changeFirstName : function (name) {
            this.firstName = name;
        }
    });
    
    var jordan = Person.create('Jordan', 'Parmer');
    

    In the end it is not really about what “the community” seems to favour but more about understanding what the language provides to achieve a certain task (in your case creating new obejcts). From there you can decide a lot better which way you prefer.

    Module patterns

    It seems as if there is some confusion with module patterns and object creation. Even if it looks similar, it has different responsibilities. Since JavaScript only has function scope modules are used to encapsulate functionality (and not accidentally create global variables or name clashes etc.). The most common way is to wrap your functionality in a self-executing function:

    (function(window, undefined) {
    })(this);
    

    Since it is just a function you might as well return something (your API) in the end

    var Person = (function(window, undefined) {
        var MyPerson = function(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        };
    
        MyPerson.prototype.fullName = function () {
            return this.firstName + ' ' + this.lastName;
        };
    
        MyPerson.prototype.changeFirstName = function (name) {
            this.firstName = name;
        };
    
        return MyPerson;
    })(this);
    

    That’s pretty much modules in JS are. They introduce a wrapping function (which is equivalent to a new scope in JavaScript) and (optionally) return an object which is the modules API.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
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 have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.