Skip to main content Accessibility Feedback

Revealing Module Pattern

Change MyLibrary to whatever namespace you’d like to use for your library.

Source Code

Examples

MyLibrary.sayHi();
MyLibrary.sayBye();

The Boilerplate

/**
 * Revealing Module Pattern Boilerplate
 */
let MyLibrary = (function () {

	// This variable is scoped internally
	// It can't be accessed externally
	let name = 'Wall-E';

	// This is returned at the end and can be run externally
	function sayHi () {
		console.log(`Hi ${name}`);
	}

	// This can also be run externally
	function sayBye () {
		console.log(`Bye ${name}`);
	}

	// Return and functions and variables that should be accessible externally
	return {sayHi, sayBye};

})();

Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.


Find this useful? You can support my work by purchasing an annual membership.