OOP In WinJS #5 : Encapsulation

In our previous article, we discussed about Inheritance and derived classes to ensure usability of OOP Design Principles in WinJS. We'll be using Universal Windows Apps for showing the demo.

Check previous articles to know more about OOP in WinJS, if you haven't already:

Before I show you how to encapsulate functions, first let's start with explaining Encapsulation.

Encapsulation is a term where you hide methods/functions from direct outside access. It's a useful method used in many libraries. The most useful trick to use is restricting specific functions to be called directly.

Instead of direct call, you'd use the encapsulated function/method via another function which we call proxy.
Let me show an example:
Ignore the language rules and focus on these 2 objects. Car is being used by Company, Company needs to move the car but doesn't need to use GPS functionality. So what we do to hide/restrict it being used is by simply making it "private".


Actually it's not that much different from classic OOP structure. JavaScript doesn't support access modifiers but you have namespaces and you can use Namespaces as access modifiers.

If a namespace has specific functions defined, then these functions are now public and can be accessed by any code. And the opposite is private.

Lets make an example:
  1. (function ()  
  2. {  
  3.    //We will define 3 function here    
  4. }  
"Function" term is used for grouping other functions within to make them private or public. But a function can only be public if added to a namespace.

Let's add our functions:
  1.    function GetEmployeesCount()  
  2.    {  
  3.        return 10;  
  4.    }  
  6.    function GetEmployerCount()  
  7.    {  
  8.        return 1;  
  9.    }  
  11.    function GetCompanyCount()  
  12.    {  
  13.        return Number(GetEmployeesCount()) + Number(GetEmployerCount());  
  14.    }  
Two functions return a count while CompanyCount returns the sum of the functions.

Note: "Number" function is used to define a variable/function as a number.Its perfectly used while adding 2 or more numbers.

Before heading to next step, I'd like to explain which functions will be public and which ones will be private as we're not going to add private function to the namespace we will be creating next step. GetCompanyCount will be public because outside access shouldn't need to access the count of Employees and Employer functions. We're providing them in a public function named GetCompanyCount.

Now lets define our namespace:
  1. WinJS.Namespace.define("Company",   
  2.    {   
  3.         GetMeCount: GetCompanyCount   
  4.    });  
From outside, code can access only "GetMeCount" function which points to GetCompanyCount function.

As you can see below, private methods can't be accessed either:

Only the defined functions inside namespace can be called directly.

Here's the result of our basic sample:

It displays the count of the company as expected.

Encapsulation is wise to use if you're providing an API/Library and don't want functions to be called directly via code.
Read more articles on Universal Windows Platform: