Nothing is iffy with IIFE in javascript

IIFE – Immediately Invoked Function Expression (Pronounced: “Iffy”)
Okay now we know what IIFE stands for what is an IIFE and how do we use it?

(function() { })();

You might of seen one before, what it does it scope everything inside itself and when the document loads the () it is executed.

A lot of JavaScript libraries use them but very few people actually use them when writing smaller JavaScript functions.

This might sound a little confusing at first but look at it this way.

function myfunction() {
}
myfunction();

Is the same in operation as:

(function() {
})();

Although the IIFE is also marginally faster, but so marginally you would have to run it millions of times to notice the difference.

So what can we do with an IIFE that is cool?

Well lets look at JQuery, I have not long opened the black box that is JQuery, since it’s release for the first time since oh I don’t know 1.3? and it has changed quite significantly. But it still uses IIFEs!

At the start you will notice the opening goes a little like this:

(function(window,document,undefined) {

and at the end like:

})(this,document);

But why does it do this. Well you have just made a localised version of the window and the document for a start, while not immensely a speed advantage, it means you have your reference to these objects already set, so you don’t need to do:

var mydocument = document;

It’s a shortcut, beautiful and simple.

So what is undefined doing there? Well JQuery initialises itself with undefined incase some clever so and so does something like:

undefined = true;

This would really mess things up when you are checking to see if something is undefined right? The problem is that this is totally valid syntax and while it might not happen, it could throw some weird things in JQuery so it makes its own localised version so no one can tamper with it.

Great we have just seen an IIFE used as a constructor of sorts for JQuery, they can be used in all sorts of ways, like looped calls:

(function myloop() {

// Code to loop here

// Loop this after 1 second more has passed
setTimeout(myloop, 1000);
})();

The beauty of this code is that it will only loop when the code has executed compared to just using set timeout to call a function that does the code to loop, this means you don’t have to worry about the code execution time, it will run 1 second after it has finished running, beautiful and safe.

I have seen other uses of IIFE for example creating false classes, you just need to imagine what you can do and give it a go.

[update]
I have just stumbled across this: Decoding Self Invoking Anonymous Functions

Creating a template for microsoft word

This is something I keep forgetting, and I make them every now and then to document my code or some other reason like user questionnaires.

To do this in word after you have designed your document on how you want it to look, and create placeholder text as you see fit you need to convert this placeholder text to fields.

To do this select your placeholder text and press CTRL+F9, however this isn’t enough if you select all and press Shift +F9 you will notice your placeholder test vanishes.

To accomplish this add “MACROBUTTON NoMacro” before your placeholder text, select all once all placeholders are made using CTRL+A and press SHIFTF9 to hide the field placeholders and save as a .dot or a .dotx file according to your own preference.

1) Create your document how you want it to look to the user
2) Select placeholder text and press CTRL+F
3) Add “MACROBUTTON NoMacro” before your placeholder text
4) Once all placeholders are done press CTRL+A and then SHIFT+F9 to hide the macro code
5) Save as a .dotx or a .dot file extension
6) Open / Reuse / Send / do what you require

Enjoy

Hello world – Well it is a programming blog..

This is my first blog post but I will keep it short because I want to get posting real problems which I had solutions too.

Well at the moment I am a SharePoint developer, but I have been a Magento Developer, Drupal Developer, HTML 5 Developer, ASP Developer, C++ Developer, Assembly Developer, JAVA Developer… Okay you get it I am a developer.

I started my career with Kodak and I have gone through many a project from Volkswagen to hundreds of unknowns.

I hope I help at least one developer out there, as many blog posts by other developers have helped me.
Good hunting my friends.