AlgorithmsCodilityPatternsTutorials

Another very useful pattern that eases object creation process is the sandbox pattern.

The main idea behind it is to have a single object that holds all our different modules. Those modules can even be interdependent, but beware of circular references.

The benefits of this design are many. There is a single sandbox object that holds all our decorators and mixins, so there is no global object pollution. The ease of creation is obvious, as you only need to state your desired modules as dependencies, and furthermore, it nicely decouples the whole process of object creation by exposing a clean interface.

Before the rise of Javascript module loaders such as require.js, browserify, systemJs, webpack and many others, this was the way to go. Now it is not used as much, but it’s still useful when applied to smaller projects. Lets take a look at it. First you need to define a constructor that will take care of our dependencies.

Now, lets add some modules

And now if you call it like this

Allright, thanks for that beer! 🙂