Chapter 4 · Real programs

Closures

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

Functions that remember

By the end of this lesson you can:

  • make a function that keeps variables of its own between calls,
  • make two counters that count apart,
  • keep a balance that no code outside can change.
function makeCounter() {
  let count = 0;
  return () => {
    count += 1;
    return count;
  };
}
const counter = makeCounter();
console.log(counter());
console.log(counter());
console.log(counter());
function makeCounter() {
  let count = 0;
  return () => {
    count += 1;
    return count;
  };
}
const c1 = makeCounter();
const c2 = makeCounter();
console.log(c1(), c1(), c2(), c1());
function makeLapCounter() {
  // keep laps here, then return an arrow function
  // that adds 1 to laps and returns the text
}
const lap = makeLapCounter();
console.log(lap());
console.log(lap());
console.log(lap());
function makeAdder(n) {
  return (x) => x + n;
}
const add5 = makeAdder(5);
const add10 = makeAdder(10);
console.log(add5(1));
console.log(add10(1));
const fns = [];
for (let i = 0; i < 3; i++) {
  fns.push(() => i);
}
console.log(fns.map((f) => f()));
// makeMultiplier(n): return a function
// that multiplies its argument by n
const double = makeMultiplier(2);
const triple = makeMultiplier(3);
console.log(double(4), triple(4));
function makeAccount() {
  let balance = 0;
  const deposit = (amount) => {
    balance += amount;
  };
  const getBalance = () => balance;
  return { deposit, getBalance };
}
const account = makeAccount();
account.deposit(10);
account.deposit(5);
console.log(account.getBalance());
console.log(account.balance);
console.log(account);
// 1. makeScoreboard() keeps a private total
// 2. it returns { add, getTotal }
// 3. one for Ada (10, then 5), one for Bo (7)
const double = (x) => x * 2;
const addTen = (x) => x + 10;
// twice(fn): return a function that
// calls fn two times on its argument
console.log(twice(double)(3));
console.log(twice(addTen)(1));
Terminal
Step 1 of 9One counter

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)