I recently got familiar with the Revealing Module pattern and I've read quite a few articles about it.
It seems like a very good pattern and I would like to start using it in a big project I have. In the project I'm using : Jquery, KO ,requirejs, Jquery Mobile, JayData. It seems to me like it'll be a good fit for the KO ViewModels.
In specific I'd like to use THIS version of it.
One thing I could not find are disadvantages for using this pattern, is it because there aren't any (I find it hard to believe)?
What should i consider before starting to use it?
The Revealing Module Pattern (RMP) creates objects that don't behave well with respect to overriding. As a consequence, objects made using the RMP don't work well as prototypes. So if you're using RMP to create objects that are going to be used in an inheritance chain, just don't. This point of view is my own, in opposition to those proponents of the Revealing Prototype Pattern.
To see the bad inheritance behavior, take the following example of a url builder:
Setting aside the question of why you would use RMP for an object with no private components, note that if you take the returned object and override urlBase with "http://stackoverflow.com", you would expect the behavior of build() to change appropriately. It doesn't, as seen in the following:
Contrast the behavior with the following url builder implementation
which behaves correctly.
You can correct the Revealing Module Pattern's behavior by using this scope as in the following
but that rather defeats the purpose of the Revealing Module Pattern. For more details, see my blog post http://ilinkuo.wordpress.com/2013/12/28/defining-return-object-literals-in-javascript/
I read the article that @nemesv referenced me to (Thanks :)) and I thinks there is one more disadvantage that was not mentioned, so I thought I'd add it here for reference. Here is a quote from the article:
And my addition:
You can't use inheritance with this pattern. For example:
This a simple example for inheritance in js, but when using the Revealing Prototype Pattern you'll need to do this:
which will override you inheritance.
Other disadvantages with the Revealing Module Pattern include:
I'd recommend using the Definitive Module Pattern over the Revealing Module Pattern (https://github.com/tfmontague/definitive-module-pattern)