🔸 What is a closure, and how/why would you use this?#
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function.
In JavaScript, closures are created every time a function is created, at function creation time.
Whenever a function is declared in JavaScript a closure is created. inside the IIFE
Closure Scope Chain: Every closure has three scopes
Local Scope (Own scope)
Outer Functions Scope
Global Scope
// global scope
var e =10;
functionsum(a){
returnfunction(b){
returnfunction(c){
// outer functions scope
returnfunction(d){
// local scope
return a + b + c + d + e;
};
};
};
}
console.log(sum(1)(2)(3)(4));// log 20
I came across great post on closure please refer this to understand more link
Situations where you might want to do this are particularly common on the web. Much of the code written in front-end JavaScript is event-based. You define some behavior, and then attach it to an event that is triggered by the user (such as a click or a keypress). The code is attached as a callback (a single function that is executed in response to the event).
You can emulate private methods with closures. Languages such as Java allow you to declare methods as private but JavaScript does not provide a native way of doing this, but it is possible to emulate private methods using closures, this resulting the modularization
var counter =(function(){
var privateCounter =0;
functionchangeBy(val){
privateCounter += val;
}
return{
increment:function(){
changeBy(1);
},
decrement:function(){
changeBy(-1);
},
value:function(){
return privateCounter;
},
};
})();
console.log(counter.value());// 0.
counter.increment();
counter.increment();
console.log(counter.value());// 2.
counter.decrement();
console.log(counter.value());// 1.
🔸 What are the consideration that we need to avoid while working with closure#
The debounce function forces a function to wait a certain amount of time before running again. The function is built to limit the number of times a function is called.
functiondebounce(func, wait, immediate){
var timeout;// To keep track of when the event occurred
returnfunctionexecutedFunction(){
var context =this;// window / global context
var args = arguments;// additional arguments it will in array
varlater=function(){
timeout =null;
if(!immediate) func.apply(context, args);// looks for condition incase of immediate invocation
};
var callNow = immediate &&!timeout;
clearTimeout(timeout);// clear the previous timeout if any
timeout =setTimeout(later, wait);
if(callNow) func.apply(context, args);// Incase of immediate function invocation
Throttling enforces a maximum number of times a function can be called over time. For example, “execute this function at most once every 100 milliseconds.”
functiondebounce(func, wait){
var timeout;
var flag =true;
returnfunction(){
var context =this;// window / global context
var args = arguments;// additional arguments it will in array
if(flag){
later.apply(context, args);
flag =false;
}
clearTimeout(timeout);// clear the previous timeout if any