Welcome Guest! (App Version 3.1)

websitnero logo name

Encapsulation and Data Abstraction

Encapsulation and Data-Abstraction in JavaScript

Encapsulation and Data Abstraction are the other 2 tenets of Object Oriented Language. Encapsulation means all properties and method related to each other keep at one place and encapsulate it in some block (as in JS there is no other block but functional scope, so we will work for functional wrapping of related properties and methods).

Data Abstraction means that 'sensitive data represented by properties and methods' must not be accessible outside the code block related to it, thereby increasing security of data and avoiding hacks or unauthorized access to data, properties, or functions.

We will see how these two tents are very tightly coupled and closesly related in JavaScript.

The method to fulfill these tenets uses concept of -- Anonymous Functions, Immediately Invoked Functional Expression (IIFE) and Closures (after Data Abstraction one and only method to access data when required).

Example

<script>

var $a = (function(){

  var a = 10;

  var res = {
    increaseA: function(val){
      a = a + val; 
      return a;
    },
    decreaseA: function(val){
      a = a - val;
      return a;
    },
    setA: function(val){
      a = val;
		},
    getA: function(){
      return a;
    }
  }

  return res;

})();

//alert(a);//not directly accessible
//alert(a+5);//cannot access a so cannot increase value
alert($a.increaseA(55));//can access inside encapsulation via closure
alert($a.increaseA(55));//can access inside encapsulation via closure
alert($a.decreaseA(15));//can access inside encapsulation via closure
$a.setA(999);//can access inside encapsulation via closure
alert($a.getA());//can access inside encapsulation via closure

</script>

See another example of Encapsulation achieved via IIFE and data still accessed via closures as below:

Example

<script>

var $a = (function(){
  var baseSalary; 
  var houseAndRentAmount; 
  var additionalTax = 0.15;
  var totalSalary;

  houseAndRentAmount = 2000; 

  baseSalaryAfterTax = function(){
    return (0.9 * baseSalary);
  }

  houseAndRentAmountAfterTax = function(){
    return (0.8 * houseAndRentAmount);
  }

  findAdditionalTax = function(){
    return (additionalTax * baseSalary);
  }

  var res = {
    findTotal: function(val){
      totalSalary = baseSalaryAfterTax() + houseAndRentAmountAfterTax() - findAdditionalTax(); 
      return totalSalary; 
    },
    setBasicSalary: function(val){
      baseSalary = val;
    },
    setAdditonalTax: function(val){
      additionalTax = val;
    }
  }

  return res;
})();

// alert($a.baseSalary); // will return undefined - as not accessible  
// alert($a.baseSalaryAfterTax()); // throw error as function not avaialable  
$a.setBasicSalary(50000); //accessible via closure 
alert($a.findTotal()); //accessible via losure

</script>
WebsiteNero is highly optimized for learning various website developing technlogies. We try our best to add maximum modules and examples to help learn the concepts clearly and vividly. We try to present all content and examples as simple as we can removing any complexity to hurdle easy understanding. We try best provide you with worthful content, but we cannot guarantee full correctness of all the content on this site. While using this website, you agree to have read and accepted all our terms of use and conditions, cookie, and privacy policy. Copyright, 2013-2018 by websitenero.com. All Rights Reserved.