> Articles > 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.
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.
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.
Use the library name to use the classes and functions of the library.
Create a library to provide reflection capability - getting properties, getting methods and dynamically invoking the method.
The library will use namespace and only required methods will be exposed to outside world.
Usage - _Reflection.Invoke(param1, ....)
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.
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.
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.
Now the library _Reflection is available which can be used in user file.
_Reflection.jsLeaving 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.
reflection.jsWe can use them in html file. First we have to write the library file, then we can write user file.
reflectiondemo.htm