Categories: JavascriptWeb

Learn Javascript – OOPs Encapsulation with Code Examples

This article represents one of the key OOPs concepts such as Encapsulation along with code examples. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
Following are the key points described later in this article:
  • Encapsulation – Basic Concepts
  • Encapsulation – Code Examples

 

Encapsulation – Basic Concepts

As per Wikipedia page, Encapsulation is the packing of data and functions into a single component….It allows selective hiding of properties and methods in an object… In OOPs based language, access specifier such as “public”, “private” and “protected” are used to specify the access properties of member variables and methods. Following rule applies for a Class member variable and methods:

  • Without “this” keyword, the member variable and methods become private. In example below, name is public, and age is private.
    function Person() {
     this.name = "Calvin";
     age = 25;
    }
    
  • In order to make the member variable and methods public, one needs to use “this” keyword.

In order to access the value of private variable, it needs to be accessed using a public method. Take a look at example below where getAge is used to access the value of “age” which is a private variable.

function Person() {
 this.name = "Calvin";
 age = 25;

 this.getAge = function() {
        return age;
    }
}

Following rule applies for an object:

  • Be default, the member variable and method of objects are public. Take a look at the code sample below which demonstrates how methods and attributes could be access:
    var calvin = {
     name: "Calvin Hobbes",
     age: 15,
     getAge: function() {
            return this.age;
        }
    }
    

    Pay attention to the fact that “this” or “var” keyword is not needed (not supported as well; throws error) to be used with member variable and methods. However, “this” keyword is used to access the member variable within a method. If not used, it will look for local variable, “age” defined using getAge method.

 

Encapsulation – Code Examples
// Following represents class, Company, that takes two arguments such as name and age. 
// Pay attention to "this" keyword which makes the variable and method, public
function Company(name, age) {
 // Name & Age variables are public; Meaning, they can be accessed by the instance of the class
 this.name = name;
 this.age = age;
 // type is a private variable; It can not be accessed publicly
 var type = 1;

 this.getName = function() {
  return this.name;
 }

 this.getAge = function() {
  return this.age;
 }

 this.getType = function() {
  return type;
 }
}
var infosys = new Company( "Infosys Technologies");
// Below would print Infosys Technologies
console.log( infosys.name ); 
// Below would print "undefined". Note the "var" keyword. Even if you do not use "var" and define the variable as it is, it is private in nature.
// In order to access the private variable value, it needs to be accessed using a public method.
console.log( infosys.getType() ); 

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Share
Published by
Ajitesh Kumar
Tags: javascript

Recent Posts

Feature Engineering in Machine Learning: Python Examples

Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…

3 days ago

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

3 days ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

4 days ago

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

4 days ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

4 days ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

4 days ago