This is the 14th day of my participation in the August Text Challenge.More challenges in August

Closures are common in everyday use, but you may not realize that this is a closure

Before we look at closures let’s look at what is scope

1. Scope

Before ES6 we used to declare variables using var so variables that were declared inside the function were local variables, and variables that were declared outside the function were global variables

Because local variables only work within functions, different functions can use variables with the same name. Local variables are created at the start of a function and destroyed automatically after the function is finished.

In ES6, there is a new keyword for declaring variables let,const, where const is used to declare constants, while let declares variables with block-level scope

    {
        let a = 1;
     }
     
     console.log(a);
     // This is not allowed and will cause an error because there is no a variable under the global scope
Copy the code

1. What are closures

Closure – JavaScript | MDN (mozilla.org)

A combination of a function bound to (or surrounded by) references to its surrounding state (lexical environment) is a closure. That is, closures allow you to access the scope of an outer function within an inner function. In JavaScript, whenever a function is created, the closure is created at the same time the function is created.

2. Simulate private methods with closures

Programming languages, such as Java, support declaring methods private, meaning that they can only be called by other methods in the same class.

JavaScript does not have this native support, but we can use closures to simulate private methods. Private methods are not just good for limiting access to code: they also provide a powerful ability to manage global namespaces, preventing non-core methods from messing up the public interface portion of code.

The following example shows how to use closures to define public functions and give them access to private functions and variables. This approach is also known as module pattern:

 var Counter = (function() {
   var privateCounter = 0;
   function changeBy(val) {
     privateCounter += val;
   }
   return {
     increment: function() {
       changeBy(1);
     },
     decrement: function() {
       changeBy(-1);
     },
     value: function() {
       returnprivateCounter; }}}) ();console.log(Counter.value()); /* logs 0 */
 Counter.increment();
 Counter.increment();
 console.log(Counter.value()); /* logs 2 */
 Counter.decrement();
 console.log(Counter.value()); /* logs 1 */
 ​
Copy the code

3. Performance considerations

It is unwise to create functions in other functions that do not require closures for some specific task, because closures have a negative impact on script performance in terms of processing speed and memory consumption.

For example, when creating a new object or class, methods should usually be associated with the object’s prototype rather than defined in the object’s constructor. The reason is that this causes the method to be reassigned each time the constructor is called (that is, for each object created, the method is reassigned).

A memory leak is when variables that you don’t need (or can’t access) still occupy memory space and can’t be reused.