GiorgioMartini
GiorgioMartini

Reputation: 123

Are the module pattern and revealing module pattern in Javascript only useful when creating API's?

Is it correct to say that the module pattern and revealing module pattern are most useful in cases like when you are building an API, where you want to have private inaccessible data ?

And for other cases where you don't care about that is more convenient to just use a constructor function ?

Or does the way you can write code inside a module pattern, without so much use of the this keyword, makes the module pattern a better choice even if you don't use private stuff?

Thanks.

Upvotes: 1

Views: 136

Answers (1)

Sebastien Daniel
Sebastien Daniel

Reputation: 4778

The module/revealing module patterns are used for encapsulation. The very concept of encapsulating your implementation details means that you're choosing to expose a specific API, to make working with the module simpler.

Constructors are nice if you can leverage javascript's prototypal inheritance, which will allow your instances to "reuse" prototype methods.

As a note:

You seem to say that creating APIs is a rare event in software development. I'd argue you're almost always creating some form of API, because that's how different parts of your application will be interacting with one another: through their APIs. Knowing only the API, and ignoring the implementation details of each module, will greatly simplify maintenance of your code base.

Upvotes: 2

Related Questions