ascott1 / ethical-web-dev

A series of short books on ethical web development
https://ethicalweb.org
191 stars 11 forks source link

Explain IIFE #19

Closed contolini closed 8 years ago

contolini commented 8 years ago

Non-JS folks may wonder what all the weird parens are doing in the following example:

(function() {
  if (!('visibilityState' in document)) {
    return false;
  }

  // rest of your code
}());

Might be worth adding a footnote explaining that the IIFE is unrelated to the visibilityState check.

ascott1 commented 8 years ago

Makes sense. I added the following as what will appear as a "note" in the final version of the book:

Note: The example above is wrapped in an Immediately-Invoked Function Expression (IIFE), which may look a bit odd if you're not a JavaScript developer. This ensures that the code executes immediately while avoiding the pollution of the global scope. If you are interested in learning more Ben Alman has written a very detailed post about IIFEs.

contolini commented 8 years ago

:+1: