SlideShare a Scribd company logo
1 of 8
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 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
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,
noGlobal ) {
//more code here
}));
- jQuery library code
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
lDetailed discussion
@http://stackoverflow.com/questions/3755606/what-
does-the-exclamation-mark-do-before-the-function

More Related Content

Similar to Iffy

Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery PluginRavi Mone
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCFAKHRUN NISHA
 
JShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java PlatformJShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java PlatformJavaDayUA
 
Unbundling the JavaScript module bundler - DublinJS July 2018
Unbundling the JavaScript module bundler - DublinJS July 2018Unbundling the JavaScript module bundler - DublinJS July 2018
Unbundling the JavaScript module bundler - DublinJS July 2018Luciano Mammino
 
Java - A broad introduction
Java - A broad introductionJava - A broad introduction
Java - A broad introductionBirol Efe
 
Knockout mvvm-m4-slides
Knockout mvvm-m4-slidesKnockout mvvm-m4-slides
Knockout mvvm-m4-slidesMasterCode.vn
 
OOP with Java
OOP with JavaOOP with Java
OOP with JavaOmegaHub
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Steven Smith
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0Michael Vorburger
 
Alive and Well with Java 8
Alive and Well with Java 8Alive and Well with Java 8
Alive and Well with Java 8Adam Pelsoczi
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzJAX London
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxkristinatemen
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedGaurav Maheshwari
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Savio Sebastian
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedyearninginjava
 
Java programming basics
Java programming basicsJava programming basics
Java programming basicsHamid Ghorbani
 

Similar to Iffy (20)

Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
 
JShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java PlatformJShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java Platform
 
Unbundling the JavaScript module bundler - DublinJS July 2018
Unbundling the JavaScript module bundler - DublinJS July 2018Unbundling the JavaScript module bundler - DublinJS July 2018
Unbundling the JavaScript module bundler - DublinJS July 2018
 
Java - A broad introduction
Java - A broad introductionJava - A broad introduction
Java - A broad introduction
 
Knockout mvvm-m4-slides
Knockout mvvm-m4-slidesKnockout mvvm-m4-slides
Knockout mvvm-m4-slides
 
OOP with Java
OOP with JavaOOP with Java
OOP with Java
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
Alive and Well with Java 8
Alive and Well with Java 8Alive and Well with Java 8
Alive and Well with Java 8
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian Goetz
 
JavaScript-Core
JavaScript-CoreJavaScript-Core
JavaScript-Core
 
JavaScript-Core
JavaScript-CoreJavaScript-Core
JavaScript-Core
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experienced
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experienced
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 

Iffy

  • 1. IIFE – Javascript Immediately-invoked function expression A summary by Suresh Koya
  • 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. Basic Example (function() { // the code here is executed once in its own scope }());
  • 4. Usage - 1 (function( global, factory ) { // code here } (typeof window !== "undefined" ? window : this, function( window, noGlobal ) { //more code here })); - jQuery library code
  • 5. Usage- 2 !function() { // More code here }(); - d3 library code
  • 6. Usage - 3 +function ($) { // Some code here }(jQuery); - bootstrap library code
  • 7. Usage - 4 ;(function() { //Code here }.call(this)); - Lodash library code
  • 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