Modular programming with ES5

Introduction

es5

The ES5 version of JavaScript does not support modules, but regardless of that it is possible to create applications in the manner of modular programming by following certain principles. Modules with their encapsulated code are created using “Immediately-Invoked Function Expressions” or with the constructor function. We simply return the API of the module that should be public inside a function with the reserved word return. This is a simple template and can be deployed anywhere without additional libraries. It is possible to define several modules within one file.

In addition to the above advantages, this approach has its disadvantages:

  • “pollution” of the global domain with module names, although the body of the module is hidden using functions (IIFE) in the local domain.
  • “manually” determining the loading order of modules and the loading order of modules can be quite complicated in large and complex applications.
  • no asynchronous loading possible

These templates are not perfect and have their flaws, but regardless of the flaws, the code is more understandable because it is better organized.

IIFE – Immediately-Invoked Function Expressions

Definition

IIFE (pronounced “ifi”) is a function expression that is executed immediately after creation. It is created by wrapping the “function declaration” with a pair of parentheses and creating a “function expression”. The reason for transforming “declared function” to “function expression”, is that a declared function cannot be called immediately in the same statement while a function expression can.

After creating the function expression in the same command, we add another pair of parentheses and thus immediately call the function.

Data privacy within the IIFE

IIFE is used for the modular programming pattern because all code inside such a function remains private, as seen in the following example.

Passing variables to IIFE

Global variables can be passed inside a function, when calling a function as an argument to that function:

The jQuery library also uses this approach to pass a variable:

Return data

With IIFE we can choose which data we want to keep private and which data we want to make public. Those parts of the code that are returned with the return keyword will be publicly available. With the mentioned method, we can pass all types of data to the global namespace. In the following example, we return the variable and thus make it globally visible:

we can return the function

Or we can return the entire object

Revealing module pattern

Most often, it is not necessary to return the entire object, but it is enough to reveal to the public only some parts of the code, which is implemented through the “Revealing module pattern”. With this principle, we can choose which methods we want to be private and which to be public and available to other modules.

IIFE + Singleton template

app layout

General

In software engineering, the singleton (Serb. unique) programming pattern is based on the principle that each object (class) has ONLY ONE INSTANCE, with the condition that access to that instance is globally available. In JavaScriptthis is possible by assigning IIFE to a global variable. Methods that we want to be globally available are “revealed” in modules following the “Revealing module pattern”. With IIFE, a local scope is created and pollution of the global domain is prevented, but the name of each module becomes part of the global domain and may collide with an external library if its variable has the same name.

Practical application in the application

index.html

This file forms the basis of the application and serves as a view for collecting data from the user and returning the results. Pay attention to the part responsible for loading modules, because the order of loading modules depends on the functionality of the application. This is the most difficult part for a developer, especially for large and complex applications. In this example, “input.js” is loaded first because its methods are used in both “proracun.js” and “app.js”, while the script “proracun.js” is loaded immediately after it but before “app.js” because its method is an integral part of “app.js”.

input.js

budget.js

app.js

Template constructor

General

The constructor template allows creating multiple instances based on the constructor function. The code changes compared to the previous template are minor. The body of the function remains unchanged while only the following things are adjusted:

  • The name of the variable to which the function is assigned should start with an uppercase letter, to satisfy the convention
  • IIFE is transformed into a regular function, by deleting the parentheses that call the function immediately because the standard constructor function is planned to be called only with the reserved word “new”
  • Inside a module that requests a method from another module, a new object needs to be instantiated, so that its method is available.

Practical application in the application

input.js

This module does not use methods from other modules, so the changes are only related to removing function call brackets.

budget.js

Inside this module it is necessary to instantiate a new “Input” object.

app.js

Inside this module it is necessary to instantiate a new “Input” and “Calculation” object.