Introduction

Encapsulation is one of the main concepts in object oriented programming. It allows an object to group both private and public members under a single name. All the object oriented programming languages support this. Since JavaScript is also an object oriented programming language, it supports it too.

In this article, we will see how to achieve encapsulation in JavaScript by creating a simple object named CssManager that helps to dynamically add, remove, or swap a style-sheet.

Plan

Create a namespace named Managers that wraps our object.

Create a singleton object named CssManager.

Add public methods to the object for adding, removing, and swapping a stylesheet.

Create private members.

Code

Create a namespace named Managers that will wrap our object

In JavaScript, creating a namespace is as simple as creating an object literal.

var Managers = {}; //namespace

Very simple, right?

Now you can wrap all the classes and objects related to Managers in a single namespace.

Create a singleton object named CssManager

Managers.CssManager = { //singleton
}

The code between the pair of curly braces forms the part of the object.

Add public methods to the object for adding, removing, and swapping a stylesheet

In JavaScript, an object acts much like an associative array, i.e., an array of key-value pairs. The key is a property or a method.

Create the method addStyleSheet that takes two parameters: the stylesheet element ID, and the reference URL.

In removeStyleSheet, we query for the particular stylesheet element (link) and remove it completely from DOM. While in swapStyleSheet, we are calling the other two methods to replace an existing stylesheet with a new one.

Create private members

So far, all the methods created are public. I would like to add two private members for the sake of the article. The first one is a variable that has the reference to the document object.

var doc = document;

The second one is a method that sets multiple attributes to the link element at once instead of calling setAttribute() multiple times.

var setAttributes = function(attributes){
}

Unlike other languages, we can't easily mark members as public or private in JavaScript. To create private members, we have to use Closures. Closures help to create a private space. We are not going to see what closures are here, but let's see how they help to create private members.

What we have done is created a Closure that looks like a self executing anonymous function that returns the public methods we added earlier. The pair of parentheses at the end makes the code get executed automatically. All the members returned by the return statement are public, while the ones that come before are private.

That's all! We have done it!! Thanks for reading this. If you feel this article is helpful to you, don't forget to vote. Please check out the attached sample code that shows how the CssManager helps to dynamically change the theme of a page.

Share

About the Author

I'm a software developer from south tip of India. I spent most of the time in learning new technologies. I've a keen interest in client-side technologies especially JavaScript and admire it is the most beautiful language ever seen.

I like sharing my knowledge and written some non-popular articles. I believe in quality and standards but blames myself for lagging them.