CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns

Writing 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.

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 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 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 JavaScript Library - Literal object to provide library interface

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


Building 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.


Building 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.


Building JavaScript Library - HTML file to have library and user file


Compiling tools.cpp

Next Article : Reflection In JavaScript

Suresh Kumar Srivastava is founder of online learning site 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.

Yearly Subscription

Advanced JavaScript

Design Patterns Through JavaScript




Knowledge Hub

Design Patterns

Tech Articles

About Us


Contact Us


CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy