Step 3: Initialize the Application

Let’s take a look at how to initialize the web application.

We'll cover the following

Initialization

We initialize the application by defining its namespace and MVC sub-namespaces. Namespaces are an important concept in software engineering and many programming languages. They help to group related pieces of code and avoid name conflicts. Java and PHP provide specific support for namespaces.

Since there’s no specific support for namespaces in JavaScript, we use special objects for this purpose. We’ll call them “namespace objects”.

First, we define a root namespace (object) for our application. Then we define three sub-namespaces, one for each of the three parts of the application code: model, view, and controller.

In the case of our example application, we can use the following code for this:

var pl = { m:{}, v:{}, c:{} };

Here, the main namespace is defined to be pl, standing for “Public Library,” with the three sub-namespaces—m, v, and c—being initially empty objects. We put this code in a separate file, initialize.js in the c folder, because such a namespace definition belongs to the controller part of the application code.

Get hands-on with 1400+ tech skills courses.