Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Keeping State in a Closure

Keeping State in a Closure

Every climber at the desk gets a numbered wristband: M-101 in the morning, L-101 in the evening Learn to Boulder class. In Python, make_band_counter() is a factory that keeps the last number in a variable, rebound with nonlocal for each band. Port it to JavaScript.

In the JavaScript port, number becomes a let, and the arrow is returned where it’s written. Watch out for the nonlocal line: as with global, JavaScript has none, so nonlocal number; stops the file with SyntaxError: Unexpected identifier 'number' before anything runs. Leave it out, and the rest ports as it is:

Language: JavaScript
function makeBandCounter(prefix, start = 101) {
  let number = start - 1;
  return () => {
    number += 1;
    return `${prefix}-${number}`;
  };
}

const morning = makeBandCounter("M");
console.log(morning());
console.log(morning());

Run it: Ana gets M-101, and Ben the next one:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Every climber at the desk gets a numbered wristband: M-101 in the morning, L-101 in the evening class. In Python, make_band_counter is a factory that keeps the last number in a variable, rebound with nonlocal for each band.

00:15 Port it to JavaScript.

00:18 In the JavaScript port, number becomes a let, and the arrow is returned where it’s written. Watch out for the nonlocal line: as with global, JavaScript has none, so a ported one stops the file with a syntax error before anything runs.

00:33 Leave it out, and the rest ports as it is. Run it: Ana gets M-101, and Ben the next one. As with the day’s check-in count, an assignment finds the nearest declared variable, so the arrow’s plus-equals updates the number its factory declared, with no nonlocal line.

00:55 The call that made morning is over, but its variables stay, because the arrow still uses them. Each call of morning adds one to that same number.

01:04 The arrow is a closure, as in Python; only the update differs.

01:11 For the class, the desk calls the factory again with "L". That call makes new variables, so Mia gets L-101 while the morning carries on with M-103: two sessions never share a number.

01:25 Log morning, and JavaScript shows a function with no name, and no number. To also count the bands issued, Python might write a small class, with the number on self.

01:39 JavaScript code often keeps the factory instead. The arrow becomes next, stored in a constant, and issued counts the bands. The factory returns both in one object, in shorthand: a property named like its variable is written once.

01:55 Call next twice, and issued gives 2. Python’s object shows its state to any code that looks, underscore or not.

02:06 Log the JavaScript object, and what does it show? Your turn: pause the video and predict.

02:18 Only the two functions. The number is a variable of the call, not a property: only next and issued can reach it.

02:28 Python’s closure keeps its number out of sight too; JavaScript reaches for a factory where Python reaches for a class: keep state in its variables, return an arrow or an object of arrows, and let callers use the functions.

02:43 Next up is Making Functions in a Loop. Each wall needs a ready message, and you’ll see how Python’s default argument trick fares in JavaScript.

Become a Member to join the conversation.