How To Implement Private & Privileged Functions in JavaScript 2020

How To Implement Private & Privileged Functions in JavaScript 2020

Javascript is object-oriented, so it has private and public members. In this post, we discuss How To Implement Private & Privileged Functions in JavaScript

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.

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 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.

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 declared using this keyword is called a privileged function. In the below code, previlageFunction is an example of 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  

Private Functions

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

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

Thank for reading!

Functional Programming for JavaScript Developers

Functional Programming for JavaScript Developers

Functional Programming for JavaScript Developers. Learn Functional Programming and how it enables developers to move from imperative to declarative programming. Solidify your knowledge of functional JavaScript programming, including objects, arrays, and prototypes. Learn higher-order functions, closures, scope, master key functional methods like map, reduce and filter and promises and ES6+ asynchronous JavaScript. Learning how to build and manage asynchronous functional programs is perhaps the most important part of becoming an effective JavaScript programmer.

Functional Programming for JavaScript Developers

Learn functional programming and how it enables developers to move from imperative to declarative programming

Improve your code with functional patterns like pure functions, compose & map/reduce/filter...plus advanced concepts like fusion, transducing and monads!

Solidify your knowledge of functional JavaScript programming, including objects, arrays, and prototypes

Learn higher-order functions, closures, scope, master key functional methods like map, reduce and filter and promises and ES6+ asynchronous JavaScript. Go beyond the fundamentals of asynchronous JavaScript and use features and techniques that will help you reduce code and create smarter applications.

Learning how to build and manage asynchronous functional programs is perhaps the most important part of becoming an effective JavaScript programmer.

What you'll learn

  • On the surface, this course is designed for beginning and intermediate JS developers who want to learn the fundamentals in order to understand and use functional programming in both ES5 and ES6. However, this course is also perfect for people preparing to enter into competitive JavaScript bootcamps