Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
IIFE – Javascript
Immediately-invoked function
expression
A summary by Suresh Koya
Why Iffy matters?
l To avoid variable hoisting from within blocks,
protect against polluting the global environment
and si...
Basic Example
(function() {
// the code here is executed once in its own scope
}());
Usage - 1
(function( global, factory ) {
// code here
} (typeof window !== "undefined" ? window : this, function( window,
...
Usage- 2
!function() {
// More code here
}();
- d3 library code
Usage - 3
+function ($) {
// Some code here
}(jQuery);
- bootstrap library code
Usage - 4
;(function() {
//Code here
}.call(this));
- Lodash library code
Summary
lWell these are just syntactic sugars. The reason
to use is to avoid variable conflicts and use
namespacing
lDetai...
Upcoming SlideShare
Loading in …5
×

Iffy

780 views

Published on

  • Be the first to comment

  • Be the first to like this

Iffy

  1. 1. IIFE – Javascript Immediately-invoked function expression A summary by Suresh Koya
  2. 2. Why Iffy matters? l To avoid variable hoisting from within blocks, protect against polluting the global environment and simultaneously allow public access to methods while retaining privacy for variables defined within the function. Additional information @https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
  3. 3. Basic Example (function() { // the code here is executed once in its own scope }());
  4. 4. Usage - 1 (function( global, factory ) { // code here } (typeof window !== "undefined" ? window : this, function( window, noGlobal ) { //more code here })); - jQuery library code
  5. 5. Usage- 2 !function() { // More code here }(); - d3 library code
  6. 6. Usage - 3 +function ($) { // Some code here }(jQuery); - bootstrap library code
  7. 7. Usage - 4 ;(function() { //Code here }.call(this)); - Lodash library code
  8. 8. Summary lWell these are just syntactic sugars. The reason to use is to avoid variable conflicts and use namespacing lDetailed discussion @http://stackoverflow.com/questions/3755606/what- does-the-exclamation-mark-do-before-the-function

×