0

The Title is a very shorthand to the actual comparison I will give below. I'm delving into JavaScript and also read about the Module Pattern (JavaScript Module Pattern: In-Depth and Mastering the Module Pattern)

An implementation of my own of the Module Pattern was slightly different than the described pattern in the links above.

My implementation:

var smath1 = new function () {
 // "private fields"
 var answer = new Number(42);

 // "public fields"
 this.PI = new Number(3.141592653589793);

 // "private functions"
 function getHalfTheAnswer() {
  return answer / 2;
 };

 // "public functions"
 this.sum = function (a, b) { return new Number(a) + new Number(b) };
 this.mul = function (a, b) { return new Number(a) * new Number(b) };
 this.getTheAnswer = function () { return answer; };
 this.calcTheAnswer = function () { return getHalfTheAnswer() * 2 };
}

I'm interested in how this is different (technically/logically) from the Pattern described in the links...

var smath2 = (function () {

 var my = {};

 // "private fields"
 var answer = new Number(42);

 // "public fields"
 my.PI = new Number(3.141592653589793);

 // "private functions"
 function getHalfTheAnswer() {
  return answer / 2;
 };

 // "public functions"
 my.sum = function (a, b) { return new Number(a) + new Number(b) };
 my.mul = function (a, b) { return new Number(a) * new Number(b) };
 my.getTheAnswer = function () { return answer; };
 my.calcTheAnswer = function () { return getHalfTheAnswer() * 2 };

 return my;
}());

...and what makes the second approach superior/preferable to the first, if so at all?

The usage would be exactly the same as well as the behavior as far as I could see in my tests. See the fiddle: JS Module Pattern

markus s
  • 1,024
  • 1
  • 11
  • 20
  • Didn't know that you can use `new function () {` always learning something new. – jcubic Sep 29 '16 at 12:00
  • 1
    The only difference I can note here is that `smath1` will have a constructor that can be used to create more objects of the type (Example: `smath3 = new smath1.constructor`), while with `smath2`, you cannot do that (you'll get an empty object). – techfoobar Sep 29 '16 at 12:12
  • I for myself think that in smath1 private variables are direct members of the prototype and can be accessed somehow? from the outside whereas in smath2 only "my" can see the private members through it's wrapping closure. – markus s Sep 29 '16 at 12:26
  • @MattWay: Thank you, so I see that smath1 is essentially a namespace/module by function and smath2 is a namespace/module by object wrapped in an anonymous function closure for variable privacy. As I'm not exactly sure I will not post this as an answer - maybe someone with a better understanding can explain this in full correctness and post it as an answer I can eventually accept. – markus s Sep 29 '16 at 12:50
  • @jcubic There are good reasons to avoid it, so better forget it right away :-) – Bergi Sep 29 '16 at 13:58
  • @markuss No, there's no differences for "private" variables between the two patterns, they're local variables closed over by the methods in both. There are no members on the prototype. – Bergi Sep 29 '16 at 14:01

0 Answers0