Javascript Practice Day2 : Private ,Public(Prototype), Privilege(this)

Private ,Public and Privileged  members in a Javascript constructor function

Private Members - Private members in a javascript can be created by using the 'var' keyword while declaring the member i.e variables and functions . The scope of the members is within the constructor function

function student(firstName ,lastName)
{
    //private field and function
    
    var dateOfBirth;
    
    //private function
    var getDOB=function(){
        dateOfBirth='01/02/2017';
        return dateOfBirth;
    }
}

Public  member - These members variable are declared with 'this' Keyword

Practice.TeamA.customer= function(firstName ,lastName)
{
    //public field
    this.firstName=firstName;
    this.lastName=lastName;
    //public field

    //privilege function
    this.getFullName=function(){
        return this.firstName+" "+this.lastName;
    }
     //privilege function
    return this;
    
}


Privilege function - These members functions are declared with the 'this' keyword .As shown in the below constructor function . Then can access the private methods .They can be accessed outside the 
constructor function

Practice.TeamA.customer= function(firstName ,lastName)
{
    //privilege function
    this.getFullName=function(){
        return this.firstName+" "+this.lastName;

     //privilege function

    }
}


Public  : Public member functions in a Javascript are declared by the prototype keyword. Public methods can access the privilege methods but cannot access the private methods .They can be accessed outside the
constructor function

//constructor function
function student(firstName ,lastName)
{
    //private field and function
    
    var dateOfBirth;
    
    //private function
    var getDOB=function(){
        dateOfBirth='01/02/2017';
        return dateOfBirth;
    }
    
    
    //previlidgeFunction - to create a previlidge function we use a this keyword
    this.prevelidgeFunction= function(){
        return getDOB();
    }
    
    //prototype -public function
    student.prototype.publicdateOfBirth=function(){
        return this.prevelidgeFunction();
    }
}

Detailed Code


function student(firstName ,lastName)
{
    //public field
    this.firstName=firstName;
    this.lastName=lastName;
    
    
    //private field and function
    
    
    
    var dateOfBirth;
    
    //private function
    var getDOB=function(){
        dateOfBirth='01/02/2017'
        return dateOfBirth;
    }
    
    
    //previlidgeFunction - to create a previlidge function we use a this keyword
    this.prevelidgeFunction= function(){
        return getDOB();
    }
    
    //prototype -public function
    student.prototype.publicdateOfBirth=function(){
        return this.prevelidgeFunction();

    }
}


var student1= new student("John","Rambo");
console.log(student1.publicdateOfBirth()+"public function");
document.write(student1.publicdateOfBirth() + "<br/>");
console.log(student1.prevelidgeFunction()+"private function");
document.write(student1.prevelidgeFunction() + "<br/>");

HTML

  <script type="text/javascript" src="TeamA.js"></script>
 <!--   <script type="text/javascript" src="TeamB.js"></script> -->
    
    <body>
        <p>Hello World</p>
        <div id="result">
            <script type="text/javascript">
            
            </script>
        </div>    
  </body>



Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6