Skip to main content

Command Palette

Search for a command to run...

[JavaScript] More Closure Examples

Published
•View as Markdown
[JavaScript] More Closure Examples
// 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

closure 2.PNG

Example 2

closure 3.PNG

JavaScript

Part 1 of 50

More from this blog

Jayden's Blog

166 posts