1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How To Implement Private & Privileged Functions in javaScript 2020

Discussion in 'JavaScript and AJAX' started by Sagar Jaybhay, Feb 10, 2020.

  1. Sagar Jaybhay

    Sagar Jaybhay New Member

    Joined:
    Jan 28, 2019
    Messages:
    29
    Likes Received:
    17
    Trophy Points:
    3
    Gender:
    Male
    Occupation:
    Sr. Software Developer
    Location:
    Pune
    Home Page:
    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.
    Code:
    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

    Code:
    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;
        }
    
    }
    
    Privileged Function:

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



    Code:
    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();
        }
    }
    
    So 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.

    Code:
    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
    
    
    Private Functions:
    This function can only be called by the privileged function. Below is the syntax of creating private function

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

Share This Page