Public and Private Methods in JavaScript

Public Members can be accessed anywhere using the "." notation.
 
Private Members can only be accessed within a constructor and public method.
 
Public methods and variables are declared using the "this" keyword like "this.variableName".
 
A Public method is accessed using a constructor; this technique is usually used to initialize public instance variables.
 
var objPublicPrivateClassg = new PublicPrivateClass();
 
Code
  1. /*Comment   
  2. <CodeItem type="Method">Class Name</CodeItem>   
  3. <summary>Define the Class name as a function</summary>   
  4. <params>   
  5. </params>   
  6. <return></return>   
  7. Comment*/    
  8. function PublicPrivateClass() {    
  9.     //PublicMethods    
  10.     this.publicMethods = {    
  11.         /*Comment   
  12.         <CodeItem type="Method">InitializePageControlEvents</CodeItem>   
  13.         <summary>Initialize the control</summary>   
  14.         <params>   
  15.         </params>   
  16.         <return></return>   
  17.         Comment*/    
  18.         InitializePageControlEvents: function () {    
  19.             privateMethods.PrivateTestMethod();    
  20.         }    
  21.     };    
  22.     // Private Methods    
  23.     privateMethods = {    
  24.         /*Comment   
  25.         <CodeItem type="Method">PrivateTestMethod</CodeItem>   
  26.         <summary>Show the alert Message</summary>   
  27.         <params>   
  28.         </params>   
  29.         <return></return>   
  30.         Comment*/    
  31.         PrivateTestMethod: function () {    
  32.             alert("Calling Private Method.");    
  33.         },    
  34.      
  35.         PrivateTestMethod2: function () {    
  36.             alert("PrivateTestMethod2");    
  37.         }     
  38.     };    
  39. }   
HTML Code
 
Calling a Public Method:
  1. <script type="text/javascript">  
  2.     function CallMethod() {   
  3.         try {              
  4.             var objPublicPrivateClassg = new PublicPrivateClass();  
  5.             //InitializePageControlEvents is Public method  
  6.             objPublicPrivateClassg.publicMethods.InitializePageControlEvents();   
  7.         }  
  8.         catch (err) {  
  9.    
  10.             alert(err.message);  
  11.         }   
  12.     }  
  13. </script> 
HTML Code
 
Calling a Public Method:
  1. <script type="text/javascript">  
  2.     function CallMethod() {   
  3.         try {  
  4.               
  5.             var objPublicPrivateClassg = new PublicPrivateClass();  
  6.             //InitializePageControlEvents is Public method  
  7.             objPublicPrivateClassg.publicMethods.InitializePageControlEvents();   
  8.         }  
  9.         catch (err) {   
  10.             alert(err.message);  
  11.         }   
  12.     }  
  13. </script> 
Output
 
Public-and-private-methods-in-JavaScript1.jpg
 
  1. <script type="text/javascript">  
  2.     function CallMethod() {   
  3.         try {   
  4.             var objPublicPrivateClassg = new PublicPrivateClass();  
  5.             //InitializePageControlEvents is Private method  
  6.              objPublicPrivateClassg.privateMethods.PrivateTestMethod();   
  7.         }  
  8.         catch (err) {   
  9.             alert(err.message);  
  10.         }   
  11.     }  
  12. </script> 
Output
 
Output-Public-and-private-methods-in-JavaScript.jpg


Similar Articles