Encapsulation in JavaScript

In OOP, encapsulation is one of the most important concepts that allows an object to group the members of public and private classes under a single name. We use encapsulation to protect our classes against accidental or willful folly. Encapsulation means to enclose something in or as if something is in a capsule.

Now, we will see whether JavaScript supports encapsulation. If it does, we can say that JavaScript is an OOP language. Let's take a look at the following example:

var person = {
  "name" : "Harry Potter",
  "age" : 22,
};
alert(person.name);
person.name = "John";
alert(person.name);

If we run this on the console. The first alert box will print the following image:

Encapsulation in JavaScript

We changed the variable name to John. Therefore, the second alert box will be similar to the following image:

Encapsulation in JavaScript

What would happen if we accidently assigned a number to the name variable?

Assigning a number to the name variable is perfectly acceptable. As far as JavaScript is concerned, a variable can accept any type of data as its value. However, we don't want a number in the place of a name. What do we do? We can use JavaScript's encapsulation property, as follows:

var person = function () {
  var Name = "Harry Potter";
  var reg = new RegExp(/d+/);
  return { 
    "setName" : function (newValue) {
      if( reg.test(newValue) ) {
        alert("Invalid Name");
      }
      else {
        Name = newValue;
      }
    },
    "getName" : function () {
      return Name; 
    }
  }; 
}(); 

alert(person.getName());   // Harry potter
person.setName( "John" );
alert(person.getName());  // John
person.setName( 42 ); // Invalid Name; the name is not changed.
person.Name = 42;     // Doesn't affect the private Name variable.
alert(person.getName());  // John is printed again.

Now, if we run the above code on console, the first output will show a popup with Harry Potter as we only called the getName() function. The getName() function has an initial value, which is Harry Potter:

Encapsulation in JavaScript

The second output will be as follows as we changed the Name property of person to John and again called the getName() function:

Encapsulation in JavaScript

The third output will be as shown in the following as we tried to push a number to a string variable. A name cannot be an integer, therefore, Invalid Name popped up as we had a condition under the if statement:

Encapsulation in JavaScript

The fourth output will be as shown in the following as the number was not added to the person's Name property. Therefore, we will get the last data that we pushed to the Name property:

Encapsulation in JavaScript

We can now confirm that JavaScript supports encapsulation.

JavaScript also supports polymorphism and abstraction. If you would like to read about them, you can refer to the following link:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

Let's do something fun. You may have heard of the game called Hangman. We'll discuss the OOP in that game. First, let's introduce you to the game.

The player needs to guess a word. If he can guess the word correctly, he is safe; otherwise, he will be hanged. Take a look at the following image to get the clear idea about the game, as follows:

Encapsulation in JavaScript
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.124.244