Over a million developers have joined DZone.

Writing Your Own JavaScript Library: Immediate Functions

· Web Dev Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

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 by Stormpath—offering a complete, pre-built User Management API for building web and mobile applications, and APIs. Download our new whitepaper: "Build Versus Buy: Customer Identity Management for Web and Mobile Applications".


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 }}