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;
}
}
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();
}
}
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.
The Privileged function can access the private variables and private method of the constructor function.
Public methods have to access privileged methods but not private methods.
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!
#javascript #developer #programming