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. 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.

Share
Published by
Ajitesh Kumar
Tags: javascript

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

3 weeks ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

4 weeks ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

1 month ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

1 month ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

1 month ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

1 month ago