Writing Your Own JavaScript Library – Immediate Functions

March 31, 2012

no comments

Writing Your Own JavaScript Library – Immediate Functions

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;
}('hello'));
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.

Summary

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.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>