How To Implement Private & Privileged Functions in JavaScript 2020

Introduction

 
In an object-oriented language like C# or Java, there are private and public members like variables and functions. If I declared a member of the class as private, then this is available inside that class. Only outside of the class it's not accessible. Javascript is object-oriented, so it has private and public members.
 
Below is the code for the private variable.
 
Note: Private variables are declared using the var keyword inside the object. This can access only private functions. 
  1. function DisplayFullname(firstname,lastname)  
  2. {  
  3.   
  4.     // this are public fields  
  5.     this.FirstName=firstname;  
  6.     this.LastName=lastname;  
  7.   
  8.     // this is Private variable which is accessible only inside of this constructor function.  
  9.     var fullname=this.FirstName+" "+this.Lastname;    
  10.   
  11. }  
In the above code, fullname is a private variable and is not accessible outside of that function. It's accessible only inside of that function.
 
Below is the code for that private function which is accessible inside of the constructor function. 
  1. function DisplayFullname(firstname,lastname)  
  2. {  
  3.   
  4.     // this are public fields  
  5.     this.FirstName=firstname;  
  6.     this.LastName=lastname;  
  7.   
  8.     // this is Private variable which is accessible only inside of this constructor function.  
  9.     var fullname="";  
  10.   
  11.     var getFullName=function()  
  12.     {  
  13.         fullname=this.FirstName+" "+this.LastName;  
  14.         return fullname;  
  15.     }  
  16.   
  17. }  

Privileged Function

 
The function declared using this keyword is called a privileged function. In the below code, previlageFunction is an example of a privileged function.
  1. function DisplayFullname(firstname,lastname)  
  2. {  
  3.   
  4.     // this are public fields  
  5.     this.FirstName=firstname;  
  6.     this.LastName=lastname;  
  7.   
  8.     // this is Private variable which is accessible only inside of this constructor function.  
  9.     var fullname="";  
  10.   
  11.     var getFullName=function()  
  12.     {  
  13.         fullname=this.FirstName+" "+this.LastName;  
  14.         return fullname;  
  15.     }  
  16.   
  17.     this.previlageFunction=function()  
  18.     {  
  19.         return getFullName();  
  20.     }  
  21. }  

What is a Privileged Function?

  1. The Privileged function is created using this keyword and in the constructor function, public methods are created using a prototype property of the constructor function.
  2. The Privileged function can access the private variables and private method of the constructor function.
  3. Public methods have to access privileged methods but not private methods.
  4. Privileged methods are also accessible outside the constructor function. 
  1. function DisplayFullname(firstname,lastname)  
  2. {  
  3.   
  4.     // this are public fields  
  5.     this.FirstName=firstname;  
  6.     this.LastName=lastname;  
  7.   
  8.     // this is Private variable which is accessible only inside of this constructor function.  
  9.     var fullname="";  
  10.   
  11.     //private function  
  12.     var getFullName=function()  
  13.     {  
  14.         fullname=this.FirstName+" "+this.LastName;  
  15.         return fullname;  
  16.     }  
  17.   
  18.     //privileged function  
  19.     this.previlageFunction=function()  
  20.     {  
  21.         return getFullName();  
  22.     }  
  23.   
  24.     //public fucntion  
  25.     DisplayFullname.prototype.publicgetFullName=function(){  
  26.             return this.previlageFunction();  
  27.     }  
  28. }  
  29.   
  30. var dis=new DisplayFullname("Sagar""Jaybhay");  
  31. document.writeln(dis.FirstName); // Sagar  
  32. document.writeln(dis.LastName);// Jaybhay  
  33. document.writeln(dis.previlageFunction());// undefined  
  34. document.writeln("<br/>");  
  35. document.writeln(dis.fullname);//undefined  
  36. document.writeln("<br/>");  
  37. document.writeln(dis.publicgetFullName());//undefined  
Private Functions
 
This function can only be called by the privileged function. Below is the syntax of creating a private function. 
  1. var getFullName=function()  
  2. {  
  3.     fullname=this.FirstName+" "+this.LastName;  
  4.     return fullname;  
  5. }