JavaScript module pattern vs Constructor with methods defined in constructor
Asked Answered
E

2

11

From a single instance and a multiple instance point of view, why would I write all those extra lines of code following the Module Pattern vs just using a standard Constructor with methods and properties defined in the constructor body?

Module Pattern sample : http://jsfiddle.net/T3ZJE/1/

var module = (function () {
    // private variables and functions
    var foo = 'bar';

    // constructor
    var module = function () {
    };

    // prototype
    module.prototype = {
        constructor: module,
        something: function () {
        }
    };

    // return module
    return module;
})();

var my_module = new module();

console.log(my_module)


Constructor sample : http://jsfiddle.net/EuvaS/2/

function Module(){

    // private variables and functions
    var foo = 'bar';

    //public methods
    this.something = function () {

    }        
}

var my_module = new Module();

console.log(my_module);

To me the end result is pretty much the same. Both can have public properties and methods, both can have "private" variables and methods which can be accessed by the public methods.

Both will define public / prototype methods once for a singleton, both will define them multiple times for multiple instances / clones of the object.

Am I missing something? What is the difference?

Effeminacy answered 28/11, 2012 at 18:15 Comment(0)
G
18

In the first example, foo will be a static variable common to all instances of module(). Meaning, all instances will reference the same variable.

In the second example, foo is different for each Module() instance.

Apart from that, I don't see any difference.

Garald answered 28/11, 2012 at 18:48 Comment(4)
That is indeed one important difference.Effeminacy
For poops and giggles, this slight modification to the Constructor method will create static private vars AND only define our public methods once while allowing them access all private vars in the Constructor scope: jsfiddle.net/zHwQX/2Effeminacy
@Effeminacy good point, didn't see that either. So I guess overall, more functionality and smaller memory footprintGarald
@Effeminacy Is jsfiddle.net/zHwQX/2 something you made, or is that a common pattern?Landahl
P
0

Nothing special difference. But I am not sure what is the point of the module pattern in this example. You don't need to include the constructor in the module. Constructors should be used like the second style. But it is better for the methods to be defined in its prototype object unless you need to have it for each instance.

Again in terms of purpose, I do not think that the module pattern here is proper way.

Philately answered 28/11, 2012 at 19:3 Comment(2)
I got that snippet from here:briancray.com/posts/javascript-module-patternEffeminacy
@Effeminacy Well, I am not sure if his opinion is proper for the pattern. As you mentioned in your question having constructor in the module is extra work which is unnecessary, I think. I can't find any merit.Philately

© 2022 - 2024 — McMap. All rights reserved.