I'm reading the book Learning JavaScript Design Patterns recently. What I don't get is the difference between module pattern and revealing module pattern. I feel they are the same thing. Anyone can give an example?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
There are at least three different ways to implement the Module Pattern, but the Revealing Module Pattern is the only Module Pattern descendant that has an official name.
The Basic Module Pattern
The Module Pattern must satisfy the following:
But there's a lot of ambiguity in this definition. By resolving the ambiguity differently, you get variants of the Module Pattern.
The Revealing Module Pattern
The Revealing Module Pattern is the most famous and most popular of the Module Pattern variants. It has a number of advantages over the other alternatives, such as
The RMP satisfies three additional conditions in addition to those in the original:
The following example shows how it's used
If you wanted to make
name
andsayHello
private, you just need to comment out the appropriate lines in the return object.The Module Pattern with Object Literal
This is probably the oldest variant of the Module Pattern. Unlike RMP, there's no sexy official name for this variant.
It satisfies the following conditions, in addition to the original:
this
, whenever possible.In the following example, you can see how, in contrast to RMP, the function definitions are actually in the return object literal, and references to members are qualified by
this
.Note that unlke RMP, in order to make
name
andsayHello
private, the references pointing toname
andsayHello
in the various function body definitions also have to be changed.The Module Pattern with Return Object Stub
This variant also has no official name.
It satisfies the following conditions, in addition to the original:
Using our old example, you can see that public members are directly added to the stub object.
If you want to make
name
andsayHello
private as before, the references to the now-private members have to be changed.Summary
The differences between the Revealing Module Pattern and the other variants of the Module Pattern is primarily in how public members are referenced. As a result, RMP is much easier to use and modify, which accounts for its popularity. However, these advantages come at a great cost (in my opinion), which Addy Osmani alludes to in his post on the Revealing Module Pattern,
and which I've talked about in some other posts.
Short answer, In Module pattern we define functions in returning object.
In Revealing Module pattern, we define functions in closure area and only use variable names in returning object.
Doing this simplifies the code and has lot of other advantages
Just try to do something based on top comment answer. May be it is safer if call private function by passing this to it, so private function could reference public function with this variable.
I created a revealing module pattern with following code.
As you can see we can override public member.