HTML5 Zone is brought to you in partnership with:

Gil Fink, Microsoft MVP, is an expert in Web development and Microsoft data platform. He works as a senior architect at Sela Group. He is currently consulting for various enterprises and companies, where he architects and develops Web and RIA-based solutions. He conducts lectures and workshops for developers and enterprises who want to specialize in infrastructure and Web development. He is also a co-author of several Microsoft Official Courses and training kits. You can read his publications at his blog: http://blogs.microsoft.co.il/blogs/gilf. Gil is a DZone MVB and is not an employee of DZone and has posted 151 posts at DZone. You can read more from them at their website. View Full User Profile

Writing Your Own JavaScript Library: Immediate Functions

04.03.2012
| 8025 views |
  • submit to reddit

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.

Published at DZone with permission of Gil Fink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)