I struggle to understand the advantages of the revealing module pattern. Take f.e. the following code:
var Person = function(name){
this.name = name;
var priv = 'secret';
}
var OtherPerson = function(name){
var name = name;
var priv = 'secret';
return({name: name});
}
duke = new Person('john');
dust = new OtherPerson('doe');
To my knowledge OtherPerson should be a classic revealing module as I found it in various resources in the web. So what is the difference between Person and OtherPerson?
I personally think that Person looks a lot cleaner and you can see your private and public variables more easily.
Well,
duke
is a Person
dust
is not an OtherPerson
In JavaScript:
instanceof duke === Person // true
instanceof dust !== AnotherPerson // true
The Person
pattern might be useful for building an object that will be instantiated, and that can also be a module. The OtherPerson
constructor, on the other hand, only returns a simple JavaScript object, so there is no sense to instantiate it later. Yet, a module that is not an object constructor can use this pattern (for example, a function that uses other locally defined data).