> Articles > Writing a JavaScript Library

Writing a JavaScript Library

In this article, we will explain writing a JavaScript library.

Library is a collection of functionality in form of code which can be reused.

What is Library?

Library is a collection of functionality in form of code which can be reused. So we have some common code to provide functionality. We can come up with Library to have that code and that code can be reused in application to provide the functionality. Let's see how writing a JavaScript library is done.

Implementation of Library

Here are the steps for implementing the library.

Step 1-
Write the classes, methods and functions in IIFE (Immediately Invoked Function Expression).

Step 2-
Expose the required classes, methods and functions to outside world.

Library Usage

Use the library name to use the classes and functions of the library.

Library of Reflection - Requirements

Create a library to provide reflection capability - getting properties, getting methods and dynamically invoking the method.

Design the library

Library Name - _Reflection


Parameters - Name of function
Return Value - Properties of function passed as parameter

Parameters - Name of function
Return Value - Methods of function passed as parameter

Parameters - Name of function, Name of method and any other parameter required for invoked method
Return Value - Nothing

Library Usage-

The library will use namespace and only required methods will be exposed to outside world.

Usage - _Reflection.Invoke(param1, ....)

Implementation of library

We will write code of library in Immediately Invoked Function Expression (IIFE) so that classes and functions of library will be in that function scope.

Writing a JavaScript Library - Library code in IIFE

We have global as parameter and passing this to function so global will have value of this. this is pointing to window object so global will be pointing window object.

Here is the function for dynamically invoking the method.

Writing a JavaScript Library - Function in library

Now we want to provide namespace. So we will have literal object _Reflection. This object will have classes and functions to provide interface to outside world. It will expose only required one to outside world.

Writing a JavaScript Library - Literal object to provide library interface

Now the library _Reflection is available which can be used in user file.


Writing a JavaScript Library - Code of library

Leaving the implementation of functions GetProperties and GetMethods as here purpose is to learn development of library in JavaScript.

We have the class and we want to use the library function to invoke the method dynamically. Here is the class and use of library for invoking the method.


Writing a JavaScript Library - Using JavaScript library

We can use them in html file. First we have to write the library file, then we can write user file.


Writing a JavaScript Library - HTML file to have library and user file


Compiling tools.cpp

Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his course on Advanced JavaScript.