Build Your Own JavaScript DOM Library
About Lesson

In this session we will create the Immediately Invoked Function Expression.  IIFE’s are very useful for executing one time code/script.  It also guards us from accidentally creating global variables. NOTE:  IIFE’s are also know as Self Executing Anonymous Function.

Step 1: IIFE

(function (window, undefined) {
  alert(window);
}(window));

Here, we are passing the global window object as a parameter to the IIFE.  This allows a global object to be made available as a local parameter, which kind of improves the performance slightly as method lookup is avoided.

We are also passing “undefined” to create a true undefined without being worried about other libraries overwriting it.  (Note this may not be needed as modern JavaScript  prevents this, but still a good practice).

When you save the library code and execute the function you should get an alert.

Try out the code

0% Complete