Classes in JavaScript

Introduction

 
I was busy one day, creating a Windows 8 application. I felt at one point like using a class. Actually I wanted to create a custom list of mine for fetching data from the JSON result that one of my WCF services was returning. I know JavaScript is an object-oriented programming language, but as soon as I typed in the keyword "class", Visual Studio, without even realizing the pain of my broken heart, refused to compile the code. 
 
It was really puzzling. After a few minutes of struggling and 2 to 3 cups of hot coffee, I got that long-awaited, Eureka moment. One line that attracted my attention the most was, "though JavaScript is an object-oriented programming language, it is prototype-based, not class-based". In JavaScript, everything is an object. In order to get the idea of what we are talking about, let's write some code.
 
Create a new Windows 8 application. If you are unfamiliar with how to do that, get that from here. Somewhere in the default.js file, create the following function prototype:
  1. function MyClass()  
  2. {  
  3.     this.FirstName;  
  4.     this.LastName;  
  5.     this.fullName =  function Name()  
  6.     {  
  7.            return this.FirstName + " "  + this.LastName;  
  8.     } 
The code above is a function but we can create objects of it, just like as we do with classes. The variables FirstName and LastName are the properties (so called as data members in OOP) and fullName will possess the result returned by the function Name() (called as the member function). The use of the method above is simple here; is the code block describing it's use case:
  1. var myObject = new MyClass();  
  2. myObject.FirstName = "Neelesh";  
  3. myObject.LastName = "Vishwakarma";  
  4. var fN = myObject.fullName(); 
We can create the object using the new keyword and by using them, so-called dot operator, we can access its internal properties. Calling fullName will return the combination of both FirstName and LastName, which will get stored in the variable fN. Here is the entire code of my default.js file:
  1.    (function () {  
  2.     "use strict";  
  3.    
  4.     WinJS.Binding.optimizeBindingReferences = true;  
  5.    
  6.     var app = WinJS.Application;  
  7.     var activation = Windows.ApplicationModel.Activation;  
  8.      
  9.     app.onactivated = function (args) {  
  10.         if (args.detail.kind === activation.ActivationKind.launch) {  
  11.             if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {  
  12.    
  13.                 var myObject = new MyClass();  
  14.                 myObject.FirstName = "Neelesh";  
  15.                 myObject.LastName = "Vishwakarma";  
  16.                 var fN = myObject.fullName();  
  17.                 Name.innerText = fN;  
  18.    
  19.             } else {  
  20.                 // TODO: This application has been reactivated from suspension.  
  21.                 // Restore application state here.  
  22.             }  
  23.             args.setPromise(WinJS.UI.processAll());  
  24.         }  
  25.     };  
  26.    
  27.     app.oncheckpoint = function (args) {  
  28.          
  29.     };  
  30.    
  31.     app.start();  
  32.    
  33.     function MyClass() {  
  34.         this.FirstName;  
  35.         this.LastName;  
  36.         this.fullName =  function Name() {  
  37.             return this.FirstName + " "  + this.LastName;  
  38.         }  
  39.    
  40.     }  
  41.    
  42. })(); 
As you can see, the code will only execute once, at the application startup. Also, the Name "infN" has been displayed on the UI via the paragraph tag. This is simply how classes are created and used in JavaScript. Stay Tuned!
 
Happy Reading!!!


Similar Articles