Broad Network


ECMAScript Module

ECMAScript 6

Foreword: In this tutorial I explain how to create an ECMAScript module.

By: Chrysanthus Date Published: 16 Jul 2016

Introduction

In this tutorial I explain how to create an ECMAScript module. A module is a set of commonly used functions. It may have a few variables. A module is typically a file and that file is used by programs written by different authors independently.

The following is a simple module:

    exports.numVar = 6;

    exports.fn1 = function (no)
        {
            sum = no + 1;
            return sum;
        }

    exports.fn2 = (no) =>
        {
            sum = no + 2;
            return sum;
        }

    function fn3 (no)
        {
            sum = no + 3;
            return sum;
        }

    exports.fnC = fn3;

The name of any variable or function that you want to be used in a (different) program, has to be exported. The reserved word, “exports” means the variable can be accessed in any program that uses the module. Only variables are exported. Function constructs are not exported. So, any function declaration has to be exported as in the last statement above. If you do not want any variable or function name to be exported, do not precede it with exports. exports is followed by a dot.

The extension for an ECMAScript file is .js; the extension for an ECMAScript module is still .js .

Program that Uses the Module
The following is a program code (file) that would use the above module:

    mo = require('./Modu.js');

    val = mo.numVar;  //val is 6

    answer1 = mo.fn1(2);  //answer1 is 3
    answer2 = mo.fn2(2);  //answer2 is 4
    answer3 = mo.fnC(2);  //answer2 is 5

Assume that the name of the module is Modu.js and it is in the same directory as the program file. So, you bring in the module to the program file with a statement like:

    mo = require('./Modu.js');

The module in the program file, is an object. So, mo, a name of your choice is an object. You access the variables of the module as data properties and methods (using mo and a dot).

Module Path
If the module is in a path from the root directory (c:/), then you will begin the program with a statement like:

    mo = require('/server/Modu.js');

where server is the first level sub directory and / is the root directory. If the module is in the current directory, then you will have something like:

    mo = require('./Modu.js');

Note the dot in front of / .If the module is in the parent directory, then you will have something like:

    mo = require('../Modu.js');

Note the two dots in front of / . If the module is in a sub directory to the current directory, then you will have something like:

    mo = require('./subDir/Modu.js');

Note the dot in front of / .

Constructor and Object
It is possible to have a constructor with its objects in a module. The following module code illustrates this:

    function ConstrFn()
        {
            this.num1 = 3;
            this.num2 = 4;
            this.add = function ()
                {
                    sum = this.num1 + this.num2;
                    return sum;
                }
        }

    exports.obj = new ConstrFn();

The program that would use this module is:

    mo = require('./Modu.js');

    addition = mo.obj.add();    //using object in module

That is it for this part of the series.

Chrys

Related Links

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

Comments

Become the Writer's Follower
Send the Writer a Message