Welcome Guest! (App Version 3.1)

websitnero logo name

Closures

Closures in JavaScript

There is only one reason to go for closures -- Data Abstraction achieved via IIFE. This is the most important point you need to understand. If you got this, you will get to know closures.

The concepts of Encapsulation, Data Abstraction, Anonymous Function, IIFE, and Closures are very very closesly related. You cannot try understand them in chunks. They all are mixed up almost like gel to flour.

When security of data is achieved by making an anonymous function run instantly (IIFE), there is no way to access data inside it, but there is one prooperty of JavaScript which makes it still possible -- This one property is called Closure.

Consider the following example to get more idea of it:

Example

<script>

function alpha(){
  var a = 10; 
  function beta(){
    a = a + 1; 
    return a;
  }
  return beta;
}

var b = alpha();
alert(b()); 
alert(b());
alert(b());
alert(b());
</script>
WebsiteNero is highly optimized for learning various website developing technlogies. We try our best to add maximum modules and examples to help learn the concepts clearly and vividly. We try to present all content and examples as simple as we can removing any complexity to hurdle easy understanding. We try best provide you with worthful content, but we cannot guarantee full correctness of all the content on this site. While using this website, you agree to have read and accepted all our terms of use and conditions, cookie, and privacy policy. Copyright, 2013-2018 by websitenero.com. All Rights Reserved.