OOP in WinJS #1: Namespaces

Universal Apps are currently the best option for building "game changer" Microsoft-stack apps for Mobile, Windows, IOT, Cloud and even XBOX.

For Native, you can use languages like C#, VB, C++ for building Universal Apps and Games. But, you can also use JavaScript for building Universal Apps so that the audience of this article will know all about WinJS library which is used for UWP JS apps.

WinJS is a framework that has many libraries within that lets you do everything a native app can do. But as you well know, real life is sometimes different than building apps as a hobbyist: There are rules, coding standards, and code reviews.

The point of writing article is to get you used to OOP in WinJS.

I am planning on a 6-series article:

  1. OOP in WinJS #1 : Namespaces
  2. OOP in WinJS #2 : Nested Namespaces
  3. OOP in WinJS #3 : Classes
  4. OOP in WinJS #4 : Inheritance
  5. OOP in WinJS #5 : Encapsulation
  6. OOP in WinJS #6 : Asynchronous Programming
In our first lesson, we will be creating namespaces and using them in our project.

Namespaces

Namespaces are blocks of code grouped together for easy readability primarily very useful for code reviewers and developers.

We'll be using Universal Apps, but you can also use same knowledge in Windows or Windows Phone specific projects that use WinJS library.

Let us start with creating our WinJS application!

Building Application

Run your Visual Studio 2015 (I use Community Edition here).

Click New Project, JavaScript, Windows, then click Universal.



The project will be created and default.js file opened up, but I have a better idea. Why don't we create a new JavaScript file?

Click on your js folder and Add new Item. Name it as you like (for instance, I named it "app.js").



We're going to define several namespaces to do specific jobs. Generally, these namespaces will only write some output.
  1. // Namespaces  
  2. WinJS.Namespace.define("SensorJob");  
  3. WinJS.Namespace.define("ShareJob");  
  4. WinJS.Namespace.define("ServiceJob");  
  5. WinJS.Namespace.define("UIJob");  
We've declared 4 namespaces with each one does specific jobs.

Now lets fill these namespaces with some functions:
  1. SensorJob.Job =  
  2. {  
  3.   Output: function () { return "Test Message coming from SensorJob"; }  
  4. };  
  5.   
  6. ShareJob.Job =  
  7. {  
  8.     Output: function () { return "Test Message coming from ShareJob"; }  
  9. };  
  10.   
  11. ServiceJob.Job =  
  12. {  
  13.     Output: function () { return "Test Message coming from ServiceJob"; }  
  14. };  
  15.   
  16. UIJob.Job =  
  17. {  
  18.     Output: function () { return "Test Message coming from UIJob"; }  
  19. };  
Once we called each function, they will write message in console window.

Let's call them!

First link your app.js in your default.html file:
  1. <script src="/js/app.js"></script>  
Then create a script block in default.html and call these functions:
  1. <script type="text/javascript">  
  2.       console.log(SensorJob.Job.Output());  
  3.       console.log(ShareJob.Job.Output());  
  4.       console.log(ServiceJob.Job.Output());  
  5.       console.log(UIJob.Job.Output());  
  6. </script>  
Now run the app and see the output message in JavaScript Console.



This sample was easy for you to understand how namespaces are defined and how functions are created inside these namespaces.
 
Read more articles on Universal Windows Apps:

Up Next
    Ebook Download
    View all
    Learn
    View all
    Araf Global is a software consultancy company founded in 2016 focusing on cutting edge technologies.