a function inside another function that has access to the variables and parameters of the outside function.
const add = (function () { let counter = 0; return function () {counter += 1; return counter} })();
add();
directly access the function within the other function.
it works as long as the variable is before the return because the closure was declared, but has not been called.
it works, in this case changes in scope or hoisting do not influence the behavior of the logic.
Scope chain, an example of it, how many closures can we nest is infinite as long as the correct structure of the closure is followed.
const add = (function () {
let counter = 0;
return function () {counter += 1; return function() {counter+=1; return function(){counter+=1; return counter;}}}
})();
function myFunction(){
document.getElementById("demo").innerHTML = add()()();
}
there isn't, doesn't exist because variables inside closure are block scoped so no problem with
Data hidding, encapsulation, privacy, first-class functions, and so on and so on.
Data bidding: hide global variables within functions to avoid a security breach Encapsulation: encapsulation is the idea that an object should not be directly exposed
function createAnimal(name, job) { //private variables let _name = name; let _job = job;
return { getName() { return _name; }, getJob() { return _job; }, setName(newName) { _name = newName; }, setJob(newJob) { _job = newJob; } }; }
if we create a count variable in the global scope it does not interfere with what happens in the closure. If we create another instance of the closure we now have two variables count = 0, however both act independently of the other, changing only if their methods are called, at least in the example that I show below
Adding "methods and attributes" similar to those of a class. Something like the example I put on data privacy.
function counter() { var count = 0
return { increment() {count += 1; console.log(count)}, decrement() {count -= 1; console.log(count)} }; } var x = counter() x.increment() x.increment() x.increment() x.decrement()
closures have a negative impact on script performance, including processing speed and memory consumption.