Over a million developers have joined DZone.
Platinum Partner

Writing Your Own JavaScript Library: Immediate Functions

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix.  Discover how IT departments looking for ways to keep up with demand for business apps has caused a new breed of developers to surface - the Rapid Application Developer.

Lately, I’ve decided to write a simple JavaScript library which I’m going to publish in the near future. In the meanwhile, I also decided to write about my adventures while writing the library. This post series will be dedicated to JavaScript and how to write your own JavaScript library.

Immediate Functions

One of the things that I was asked last week in the SDP Advanced JavaScript tutorial day was why I wrapped code that I wrote in an immediate function. Before I answer that question lets see what are immediate functions. In JavaScript, you can declare a function and then run it as soon as you finished declaring it. This behavior is called immediate function or in some JavaScript books “self-invoking” or “self-executing” function. Here is an example of an immediate function:

(function () { 
    console.log('inside an immediate function');

Immediate functions are used to create a scope for initialization code or for one execution of some functionality. In these situations, creating a named function is a waste of memory and therefore an immediate function is a good solution. Immediate functions can get parameters and can return values. Here is an example that include the use of parameters and return value:

var returnedValue = (function (message) {
    return message;
console.log(returnedValue); // will write hello in the console

In the example, the message parameter is set to the hello value and is returned by the immediate function. After executing the code a ‘hello’ will be written to the console.  

Writing the Library’s Immediate Function

When writing your own JavaScript library you will start with an immediate function that will initialize your library and declare its scope. You will do that because you want to create the library inside a scope which isn’t the global scope and you want to initialize its behavior as soon as possible. Here is an example of using an immediate function for wrapping a JavaScript library:

(function (ns) {
    // Your library comes here
}(this.ns = this.ns || {}));

The code is an immediate function that declares a scoped variable (ns) that is going to be used inside the library as its namespace. In the execution of the immediate function, the this keyword is used to create the namespace object in the global scope and then to pass it as a variable to be used inside the library.


In the post I explained what is an immediate function in JavaScript. I also showed how to use an immediate function to create a scope for a JavaScript library. In the next posts in this series I’ll continue to write about JavaScript concepts and how to implement them inside of JavaScript libraries.

The Web Dev Zone is brought to you in partnership with Mendix.  Learn more about The Essentials of Digital Innovation and how it needs to be at the heart of every organization.


Published at DZone with permission of Gil Fink , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}