0

I am aware of javascript module pattern but I use two types of module patterns and would like to know the difference between them from an architectural perspective.

// PATTERN ONE
var module = (function() {
 var _privateVariable = '';
 var _privateMethod = function() {
 var _this = this;
 // private method def
 // can use _this._privateVariable
 console.log('Inside a private method!');
 };
 var publicMethod = function() {
 var _this = this;
 // public method def
 // can use _this._privateVariable
 // can call _privateMethod();
 };
 return {
 publicMethod: publicMethod
 };
})();
// PATTERN TWO
var module = (function() {
 var wrapper = {
 _privateVariable: '',
 _privateMethod: function() {
 var _this = this;
 // private method def
 // can use _this._privateVariable
 console.log('Inside a private method!');
 },
 publicMethod: function() {
 var _this = this;
 // public method def
 // can use _this._privateVariable
 // can call _privateMethod();
 },
 };
 return {
 publicMethod: wrapper.publicMethod
 };
})();

Both these patterns seem to do the same thing for me

  1. Is there a significant difference in using either of them?
  2. Should one of these patterns be avoided?
  3. Is there a better way to use either of them?
asked Dec 2, 2017 at 15:13
6
  • 1
    The only difference is that you return the methods result instead of the method itself in version 2... And i prefer the second as its shorter and better structured. Commented Dec 2, 2017 at 15:33
  • keep in mind you can also use es modules, but you'll need babel for that Commented Dec 2, 2017 at 15:44
  • Your second snippet does call publicMethod() instead of "exporting" it. Commented Dec 2, 2017 at 16:22
  • var _this = this; is not meaningful in either of your patterns. You should just refer to the variables directly (in the first snippet), or to properties of wrapper (in your second snippet). Commented Dec 2, 2017 at 16:23
  • @Bergi I edited the code where I made a method call instead of export =) Commented Dec 3, 2017 at 5:24

1 Answer 1

1

In fact, there is no difference between the two patterns you've mentioned. Only difference I see is that the second pattern uses wrapper as an extra variable which can be avoided.

Considering other cases, where you might want to return a complex object rather than the current one, then the second pattern is very useful,

for eg.

var wrapper = {
_privateVariable: '',
_privateMethod: function() {
 var _this = this;
 console.log('Inside a private method!');
},
publicMethod: function() {
 var _this = this;
},
publicMethod2: function() {
 var _this = null;
},
publicMethod3: function(default) {
 var _this = default;
},
};
return {
 publicMethod: wrapper
};
answered Dec 2, 2017 at 15:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.