Simplify JavaScript Object Oriented Programming Model: Part 1

Introduction

 
Code reviews of JavaScript code is always a headache for web developers because large JavaScript code is not easy to read and understand so this article might help introduce how to write simple and readable code in JavaScript. To make things easy I am using jQuery here but I will show each thing here. 
 
It is important to note that there are no classes in JavaScript because JavaScript is a prototypal object-oriented language. This means objects inherit from objects, not classes from classes as in object-oriented languages (C#, Java). Everything is an object except for the primitive data types (boolean, number, and string) and undefined.
 
A "class" is only a concept that supports the Object-Oriented Programming model in JavaScript. When a JavaScript function is invoked as a constructor with the new keyword then that function calls a class (conceptually).
 
A road map to learn object-oriented JavaScript
An Example 
 
To show the JavaScript Object-Oriented Programming model, I am using a simple program that changes the background color when the window is loaded into the browser, in other words when the DOM is rendered in the browser. So let's see an example.
 
First of all, we create a JavaScript (js) file called page.js that contains the following code snippet.
  1. (function($) {    
  2.     function Page() {    
  3.         var $this = this;    
  4.     
  5.         function initialize() {    
  6.             $('body').css('background-color''rgba(123, 120, 234, 0.7)');    
  7.         }    
  8.     
  9.         $this.init = function() {    
  10.             initialize();    
  11.         }    
  12.     }    
  13.     $(function() {    
  14.         var self = new Page();    
  15.         self.init();    
  16.     })    
  17. }(jQuery))   
Now you are thinking, what are we doing in this code? So let's see each line of code one by one.
 

Remove jQuery Conflicts

 
When we are developing a large project it's possible to use more than one JavaScript library and it's also possible for their aliases to conflict. You can remove conflicts using the following code that shows $ alias represents jQuery, in other words, $ is doing the same as jQuery so you can also replace $ by jQuery.
  1. (function($) {    
  2.     
  3.     //code here    
  4.     
  5. }(jQuery))   

Class and Global Variable

 
I have been previously mentioned that class is a concept in JavaScript. As in the following code sample we have created some functions but we invoke the Page() function by its constructor using the new keyword. That's why it's called a class instead of a function.
  1. function Page() {    
  2.     var $this = this;    
  3.     
  4.     function initialize() {    
  5.         $('body').css('background-color''rgba(123, 120, 234, 0.7)');    
  6.     }    
  7.     
  8.     $this.init = function() {    
  9.         initialize();    
  10.     }    
  11. }    
  12. $(function() {    
  13.     var self = new Page();    
  14.     self.init();    
  15. })   
Sinice we assign this to the variable $this in this function it's an instance of it, in other words we have an object creating a Page() class and then assigns it to the $this variable. One more thing is, It's conventional in JavaScript to write the class name in Pascal case and the internal function name in Camel case to differentiate them.
 

Private and Public Function

 
As you know, the Page() is a function that is also a class but internally we create two more functions as shown in the following code snippet.
  1. function initialize() {    
  2.     $('body').css('background-color''rgba(123, 120, 234, 0.7)');    
  3. }    
  4.     
  5. $this.init = function() {    
  6.     initialize();    
  7. }   
Here initialize() is a private function because it is not access outside the Page() class so how to access it outside?
 
Since we have the $this variable that was assigned a value that is an object, we create a property such as $this.init and assign a function() to it. Since $this.init is also in the Page() class it can access the private function initialize(). So $this.init is a public function that can call outside the Page() class by its object.
 

Call on Window Load

 
As in the following code, it's also a function that creates an object of the Page() class and calls its public method init().
  1. function initialize() {    
  2.     $('body').css('background-color''rgba(123, 120, 234, 0.7)');    
  3. }    
  4.     
  5. $this.init = function() {    
  6.     initialize();    
  7. }   
This function is always called when the window loads, in other words it is similar to $(document).ready().
  1. $(function() {    
  2.     $(document).ready(function() { ==    
  3.     })    
  4. })   
The following code snippet for an HTML page has a reference of both the jQuery library and the page.js script file.
  1.     <!DOCTYPE html>    
  2.        <html xmlns="http://www.w3.org/1999/xhtml">    
  3.           <head>    
  4.              <title>index</title>    
  5.           </head>    
  6.           <body>    
  7.              <h1>Welcom JavaScript & jQuery !</h1>    
  8.           </body>    
  9.        </html>    
  10.     <script src="Scripts/jquery-1.7.1.min.js">  
  11. </script>    
  12.     <script src="Scripts/page.js">  
  13. </script>