How To Implement Private & Privileged Functions in javaScript 2020

In this article we are going to understand how declare private members in javascript means private variables and functions also we are learn Privileged functions in javascript.

Private Members In JavaScript:

In an object-oriented language like C#, Java they are having private and public members like variables and functions. If I declared a member of the class is private then this is available inside that class only outside of class it is not accessible.

Javascript is object-oriented so it’s having private and public members.

Below is code for the private variable.

Private variables are declared using var keyword inside the object and this can access only private functions.

function DisplayFullname(firstname,lastname)
{

    // this are public fields
    this.FirstName=firstname;
    this.LastName=lastname;

    // this is Private variable which is accessible only inside of this constructor function.
    var fullname=this.FirstName+" "+this.Lastname;  

}

In the above code fullname is private variable and it is not accessible outside of that function and accessible only inside of that function.

Below is code for that private function which is accessible inside of that constructor function.

function DisplayFullname(firstname,lastname)
{

    // this are public fields
    this.FirstName=firstname;
    this.LastName=lastname;

    // this is Private variable which is accessible only inside of this constructor function.
    var fullname="";

    var getFullName=function()
    {
        fullname=this.FirstName+" "+this.LastName;
        return fullname;
    }

}

Private Functions In Javascript

This function can only be called by the privileged function. Below is the syntax of creating private function

var getFullName=function()
    {
        fullname=this.FirstName+" "+this.LastName;
        return fullname;
    }

Privileged Function In JavaScript

The function which is declared using this keyword is called a privileged function. In below code previlageFunction this is a privileged function

function DisplayFullname(firstname,lastname)
{

    // this are public fields
    this.FirstName=firstname;
    this.LastName=lastname;

    // this is Private variable which is accessible only inside of this constructor function.
    var fullname="";

    var getFullName=function()
    {
        fullname=this.FirstName+" "+this.LastName;
        return fullname;
    }

    this.previlageFunction=function()
    {
        return getFullName();
    }
}

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.
function DisplayFullname(firstname,lastname)
{

    // this are public fields
    this.FirstName=firstname;
    this.LastName=lastname;

    // this is Private variable which is accessible only inside of this constructor function.
    var fullname="";

    //private function
    var getFullName=function()
    {
        fullname=this.FirstName+" "+this.LastName;
        return fullname;
    }

    //privileged function
    this.previlageFunction=function()
    {
        return getFullName();
    }

    //public fucntion
    DisplayFullname.prototype.publicgetFullName=function(){
            return this.previlageFunction();
    }
}

var dis=new DisplayFullname("Sagar", "Jaybhay");
document.writeln(dis.FirstName); // Sagar
document.writeln(dis.LastName);// Jaybhay
document.writeln(dis.previlageFunction());// undefined
document.writeln("<br/>");
document.writeln(dis.fullname);//undefined
document.writeln("<br/>");
document.writeln(dis.publicgetFullName());//undefined


GitHub :- https://github.com/Sagar-Jaybhay


Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...