Skip to content

Instantly share code, notes, and snippets.

View Calebyang93's full-sized avatar

Caleb Yang Calebyang93

View GitHub Profile
@Calebyang93
Calebyang93 / Revealing-Module-Pattern.md
Created November 5, 2019 09:25 — forked from zcaceres/Revealing-Module-Pattern.md
Using the Revealing Module Pattern in Javascript

The Revealing Module Pattern in Javascript

Zach Caceres

Javascript does not have the typical 'private' and 'public' specifiers of more traditional object oriented languages like C# or Java. However, you can achieve the same effect through the clever application of Javascript's function-level scoping. The Revealing Module pattern is a design pattern for Javascript applications that elegantly solves this problem.

The central principle of the Revealing Module pattern is that all functionality and variables should be hidden unless deliberately exposed.

Let's imagine we have a music application where a musicPlayer.js file handles much of our user's experience. We need to access some methods, but shouldn't be able to mess with other methods or variables.

Using Function Scope to Create Public and Private Methods