Google JavaScript Closures: Master the Concept for Optimal Performance
Closures are a crucial concept in JavaScript, often used to create privacy and data encapsulation. They are a feature that allows a function to have access to its own scope, the outer function’s scope, and the global scope, even when the outer function has finished execution. Understanding closures is essential for leveraging powerful techniques like currying, strengthening functions, and creating private methods.
Closures are created every time a function is created, sometime inside another function. The created function (through return or not) continues to remember and access the variables in the outer (enclosing) function. This ability to remember and access variables in the containing (enclosing) scope is what we refer to as a closure in JavaScript.
Understanding Closures
Let's start by understanding the basics of how closures are created. Each time a function is invoked, a new set of variables is created for it. Once the function is finished execution, it and its variables are expected to be garbage collected. However, due to closures, this may not happen.
Closures are created wherever we define a function inside another function. They enable functions to remember and access the variables from the parent scope even when the parent function has finished execution. This is achieved via the 'outer' function's temporary 'Scope Object', which automatically passes on 'this' (the outer function's context) and the 'name' of the outer function.
Creating a Closure
To create a closure, you need to define a function inside another function, then call the inner function.
In this example, `outerFunction` is the outer (or parent) function and `innerFunction` is the inner (or child) function. When `outerFunction` is called, it returns a reference to `innerFunction`. This returned function remembers the value of `message` from the outer scope, encapsulating it in memory, thereby creating a closure.
Accessing the Enclosed Environment
The enclosed environment can be accessed from the returned closure. When the returned closure is invoked, the enclosed details are still accessible within its scope.
```javascript var sayHello = outerFunction('Hello'); sayHello(); //logs "Hello" ```In here, `sayHello` is actually a reference to `innerFunction`, finished executing `outerFunction`, but `message` ('Hello') in the returned `innerFunction` is still accessible. This is the fundamental concept behind JavaScript closures.
Permissions and Encapsulation
Closures also allow for the creation of private methods which are only accessible within the scope of a certain function. They are indeed a powerful tool when it comes to data encapsulation and privacy.
To validate the creation of private methods, let's create a counter function which has a private variable that increments with each function call and a public method that returns the current value:
Private Methods with Closures
```javascript function createCounter() { let privateCounter = 0; function incrementCounter() { privateCounter++; } return { increment: incrementCounter, value: () => privateCounter }; } ```