CHAPTER 11 Ensuring Correct Implementation and Use of the Sandbox Pattern


  • Using the Sandbox Pattern to create loosely coupled code
  • Implementing the Sandbox Pattern using test-driven development
  • Understanding how the Sandbox Pattern controls access to resources and functionality
  • Writing and testing code that will be isolated within a sandbox


You can find the code downloads for this chapter at on the Download Code tab. The files are in the Chapter 11 download and are individually named according to the filenames noted throughout this chapter.

In his book JavaScript Patterns (O’Reilly, 2010), Stoyan Stefanov presents the Sandbox Pattern as a solution to some of the drawbacks of the practice of namespacing in JavaScript. In particular, he states that the Sandbox Pattern alleviates the reliance on a single global variable for the application and reduces the proliferation of long, dotted names to type and resolve at run time. While the Sandbox Pattern certainly does both of those things, we’re most interested in another characteristic he mentions: the creation of an environment for modules to “play” in without impacting other modules and their sandboxes.

This chapter will use test-driven development to create an implementation of the Sandbox Pattern similar to the pattern presented in Stefanov’s book. The implementation will focus on creating an extendable sandbox, ...

Get Reliable JavaScript: How to Code Safely in the World's Most Dangerous Language now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.