From the course: JavaScript: Closures

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Implement the module pattern

Implement the module pattern - JavaScript Tutorial

From the course: JavaScript: Closures

Implement the module pattern

- [Instructor] One of the main places that closures can be used in JavaScript is to create modules. The idea of a module is to encapsulate a set of code and expose just a subset of properties and methods for working with it. A module keeps some values totally protected while providing access to others in specific ways through its methods. Another advantage of using a module is that it avoids polluting the global namespace. All methods and properties are replaced with a single name, the name of the module. The code to create a module returns an object. The methods within that returned object contain closures and the code that returns the object uses an IIFE so it's invoked immediately when the JavaScript code is parsed. The return value is assigned to a variable. You can then access the methods of the returned object using the name of the variable, which becomes the name of the module. In the code for this video, in…

Contents