JavaScript: Constructors & Prototypes for Public, Private & Privaileged Methods

The Use of Constructors & Prototypes to show examples of Public, Private & Privaileged Methods

by sjmcpherson

JavaScript

function Constuct(param){
    this.publicVariable = param; //Public Instance Variable  
    var privateVariable = "A Private Variable";    
    
    function privateMeth(){
      //A private method
    }
   
    this.privilegedMethod = function(){
        return privateVariable + " Accessed by a Privileged Method";
    }
}
var myConstuct = new Constuct("Public Instance Variable");

Constuct.prototype.pubMethod = function () {
    return this.publicVariable + " Accessed by a Public Method";
}

console.log(myConstuct);
console.log(myConstuct.pubMethod());
console.log(myConstuct.privilegedMethod());