Closures:-
---------
Closer is a self invoking function which returns an inner function.
A closure is a function having access to the parent scope, even after the parent function has closed.
Advantages:-
----------
Encapsulation: Closures provide a way to encapsulate variables and functions, making it possible to create private data and methods that are inaccessible from outside the closure.
Memory efficiency: Closures allow you to create functions that can access variables and objects from their enclosing functions, without having to pass them as arguments every time the function is called. This can help reduce memory usage and improve performance.
Function factories: Closures can be used to create function factories that generate new functions with different values for specific variables. This can be useful when creating event handlers or callbacks.
Asynchronous programming: Closures are often used in asynchronous programming to capture the current state of variables and objects, allowing them to be used later when the asynchronous function returns.
Partial application: Closures can be used to create partially applied functions, which are functions that have some of their arguments pre-set to specific values. This can be useful in situations where you need to reuse a function with different parameters.
Memoization: Closures can be used to implement memoization, which is a technique for caching the results of a function so that it doesn't have to be recalculated every time it's called with the same arguments.
Overall, closures are a powerful and versatile feature of JavaScript that can be used to solve a wide range of programming problems.
Disadvantages of closure:
--------------------------
Memory usage: Closures hold onto references of their enclosing functions' variables and objects, which can lead to memory usage issues if the closure is not properly managed.
Performance: Since closures involve creating a new function object every time the outer function is called, there can be a performance impact, especially in situations where the closure is called frequently.
Difficulty in debugging: Closures can make code more complex and harder to debug, especially when dealing with complex nesting of functions.
Security: In some cases, closures can create security vulnerabilities by allowing access to private data or exposing functions to unintended users.
Maintenance: Closures can make code harder to maintain and understand, especially for developers who are not familiar with them.
--------------------------------------------
// Function to increment counter
let add = (function abc() {
let counter = 0;
return function plus(){
counter += 1;
return counter;
};
})();
// Call add() 3 times
console.log(add());
console.log(add());
console.log(add());
Another example:
function add(num) {
return function(otherNum) {
return num + otherNum;
}
}
function three() {
return 3;
}
function seven(num) {
if(num){
return num(7)
}
return 7;
}
console.log(seven(add(three()))); // Output: 10
In questa pagina del sito puoi guardare il video online Part 30 | JavaScript Tutorial | Closures in JavaScript | Self invoking function | Inner Function della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Trinits Technologies 04 marzo 2023, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 77 volte e gli è piaciuto 1 spettatori. Buona visione!