Exploring JavaScript
Closures: Practical Use
Cases Unveiled
@DimpleKumari
Forming a network of fantastic coders.
WHAT IS CLOSURE?
A closure is the
combination of a function
bundled together (enclosed)
with references to its
surrounding state (the
lexical environment). It
gives you access to an outer
function's scope from an
inner function.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 1 : Encapsulating Variables
Closures can be used to encapsulate
variables, protecting them from
external interference while still
allowing access and manipulation
through the returned function.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 1 : Encapsulating Variables
In this example, the createBankAccount function creates
an object that includes three closures: deposit, withdraw,
and checkBalance. These closures all have access to and
can operate on the initialBalance variable, which is hidden
from the outside. The account's balance is encapsulated
within the closures, and interactions with the balance are
only allowed through the methods provided, thereby
achieving encapsulation.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 2 : Modular Development
Closures play an important role in modular
development. With closures, we can create
private variables and methods, avoiding global
namespace conflicts and variable pollution.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 2 : Modular Development
In this example, we use an Immediately Invoked
Function Expression (IIFE) to create an anonymous
function scope and return an object with public
methods. The privateVariable and privateFunction
defined within the function scope are private, and
cannot be directly accessed from the outside.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 3 : Implementing Function
Currying
Currying is a technique in functional programming
where a function with multiple arguments is
transformed into a sequence of functions, each taking
a single argument. This simplifies the function
invocation process. Closures can effectively
implement function currying.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 3 : Implementing Function
Currying
In this example, we've created a generic currying
function using createCurry. It takes a function as
a parameter and returns a curried version of that
function. This curried function can be invoked
either all at once or partially, allowing for the
reuse and flexible invocation of the function.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 4: Implementing Caching
Closures can be used to implement data caching,
which is particularly useful for improving
performance in situations where functions are
called frequently.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 4: Implementing Caching
In this example, we create a cache function through
the createCache function. When we set data in
the cache using the cache function, the data is
stored in the cache Map. When retrieving data
from the cache, the cache function looks up and
returns the data from the cache Map.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 5: Implementing Private Methods
For object-oriented programming, private methods
are an important way to encapsulate data and
behavior, preventing external direct
access and modification of internal state.
Closures help us implement private methods.
@DimpleKumari
Forming a network of fantastic coders.
Use Case 5: Implementing Private Methods
In this example, we create an object
with a createPerson function, which
includes a closure function sayHello
that can access and operate on the
privateGreeting variable. The
privateGreeting variable cannot be
directly accessed from the outside,
thus ensuring the privacy of the data.
@DimpleKumari
Forming a network of fantastic coders.
Dimple Kumari
Forming a network of fantastic coders.