What is Namespace in WinJS

Let us first try to understand, why we need Namespace? I will try to explain you with an example, let us suppose you have a function named GreetingMessage in JS file named Library.js

image

Now you need to use this function on default.js. When you try to call that function from other js file , you get following exception.

 

image

Essentially, you cannot access a function or variable outside its scope. Namespace in WinJS library allows you to access a function outside its scope.

  • Namespace allows you to access a function outside its scope.
  • Namespace allows you to use a function in different files along with file it is defined.
  • Namespace helps you to organize your code
  • Any type created in Namespace can be referenced outside the scope of Namespace

Now let us see that how can we define a Namespace in WinJs . You can define Namespace by calling the define function

 

image

There are two parameters need to be passed to create Namespace.

  1. Namespace name is a string
  2. Members of the Namespace as object

You can create a Namespace in Library.js as following,


(function () {

 function GreetingMessage() {

return "Hello from WinJS";

 }

WinJS.Namespace.define("MyLibrary.Function", {
 GreetingMessage: GreetingMessage
 });

})();

There are few points you need to take care here

  • Enclosed all the Namespace definition in self executed function
  • Make sure you are not forgetting the braces at the end.
  • We have defined a function named GreetingMessage
  • We have defined a namespace and function inside that can be accessed as following

image

We have defined a Namespace and now we want to access the function GreetingMessage() on the default.js file. To do this first step you need to do is to refer MyLibrary.js on default.html file. You can refer that as following,

image 
Now you can access GreetingMessage() function on default.js as following ,

var res = MyLibrary.Function.GreetingMessage();
 var output = document.getElementById("output");
 output.innerHTML = res;

In this way by using Namespace, you can access functions and objects in different files and beyond scope of the declaration. I hope you find this post useful. Thanks for reading.

7 thoughts on “What is Namespace in WinJS

  1. Namespaces allow you to prevent global namespace (window) pollution and names collision organizing your objects inside ‘Areas’. Even without Namespace i can access function residing in other files if they are not wrapped inside a self invoking function.

  2. Pingback: Dew Drop – November 16, 2012 (#1,444) | Alvin Ashcraft's Morning Dew

  3. Pingback: Geek Reading November 16, 2012 | Regular Geek

  4. Pingback: Self Executing model is key of creating namespace in WinJS

  5. I just started with WinJS, and I am wondering, if we define our function and the namespace in MyLibrary.js, and as MyLibrary.js is included AFTER default.js, wouldn’t it still remain undefined if we try to access the function in the default.js (since technically, the function is not yet defined unless it executes until after the interpreter parses the function)

  6. I just started with WinJS, and I am wondering, if we define our function and the namespace in MyLibrary.js, and as MyLibrary.js is included AFTER default.js, wouldn’t it still remain undefined if we try to access the function in the default.js (since technically, the function is not yet defined unless it executes until after the interpreter parses the function)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s