[JavaScript] More Closure Examples
![[JavaScript] More Closure Examples](https://cdn.hashnode.com/res/hashnode/image/unsplash/ZS67i1HLllo/upload/v1655866331216/xNXHSENhd.jpeg)
// Closure Example 1
let f;
const g = function () {
const a = 23;
f = function () {
console.log(a * 2);
};
};
const h = function () {
const b = 777;
f = function () {
console.log(b * 2);
};
};
g();
f();
console.dir(f);
// Re-assigning f function
h();
f();
console.dir(f);
// Example 2
const boardPassengers = function (n, wait) {
const perGroup = n / 3;
setTimeout(function () {
console.log(`We are now boarding all ${n} passengers`);
console.log(`There are 3 groupd, each with ${perGroup} passengers`);
}, wait * 1000);
console.log(`Will start boarding in ${wait} seconds`);
};
boardPassengers(180, 3);
Example 1
Example 2