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

Watch a Function Remember

In this lesson, you watched Marco’s makeTeamScorer() make a scorer for the Know-It-Owls that keeps adding to its own total after makeTeamScorer() has returned, and a second scorer for Table Six with a total of its own.

Key Points

  • makeTeamScorer() takes a team name, sets total to 0, and returns an arrow function without calling it. The arrow doesn’t declare total or teamName. Walking outward from where it’s written, you find both one box out, in makeTeamScorer().
  • Calling makeTeamScorer("Know-It-Owls") puts a frame on the call stack, and with it a box for that call’s variables. It’s the box around the function’s code, filled in for this one call. When the call returns, its frame comes off, but the box stays where it is. The arrow it made links to that box, so JavaScript keeps it.
  • Watch out: a function’s variables aren’t wiped out when it returns. They last as long as something can still reach them.
  • Each time you call scoreOwls(), the arrow’s frame goes on the stack with its own points. The walk for total and teamName steps one box out, along its link, to the Know-It-Owls call’s box. The first call takes total from 0 to 7, and the second finds the same total, still at 7, and adds 9.
  • When a scoreOwls() call returns, its points goes with its frame, because nothing links to it. Every call you’ve followed on the stack worked that way. total stays, because the arrow still links to its box.
  • A function together with the variables it links to from where it was made is called a closure. That’s how scoreOwls() keeps the Know-It-Owls’ total from one call to the next.
  • Every call of makeTeamScorer() runs its body again, so let total = 0; makes a new total beside a new teamName. The arrow that call returns links to those.
  • Watch out: the two scorers don’t share one total. Each total belongs to one call of makeTeamScorer(), not to the function itself.
  • The rule is that a function made inside a call keeps that call’s variables, even after the call returns. Each call makes new variables, so each scorer keeps a total of its own.

Final Code

Here’s scorers.js, with a scorer for each of the two teams:

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 You’ve followed calls on the stack, and walked outward from where code is written to find a name. Here, you’ll watch the two meet, in a function that remembers.

00:10 At the Lantern, Marco wants a running total for each team. Five top-level totals would be five names to keep apart. He’d rather give each team a scorer, a function he calls with only the points.

00:22 It carries its own team and total. Here’s the function that makes them, in Marco’s scorers.js file. makeTeamScorer() takes a team name and sets total to zero.

00:34 Then it returns an arrow function, without calling it. The arrow adds its points to total and gives back the team’s name with the new total.

00:44 The arrow doesn’t declare total or the team name. Walk outward from where it’s written, and one box out, in makeTeamScorer(), you find both. But a call’s frame comes off the stack when the call returns.

00:57 So once makeTeamScorer() is done, what’s left for the arrow to find?

01:04 Line 9 calls makeTeamScorer() with the Know-It-Owls. Its frame lands on the stack. With it comes a box for this call’s variables. It’s the box you saw around the function’s code, filled in for this one call.

01:17 Line 2 runs, and total starts at zero. Line 3 creates the arrow. It’s a new function, made inside this call, so it’s linked to this call’s box. The call returns the arrow, and its frame comes off the stack.

01:33 On the stack before, a call’s variables went with its frame, because nothing linked to them. This time, the arrow still links to the box, so JavaScript keeps it.

01:43 Watch out: a function’s variables aren’t wiped out when it returns. They last as long as something can still reach them.

01:52 Back on line 9, scoreOwls holds the arrow, and the arrow holds on to the box. Line 10 calls scoreOwls() with seven. The arrow’s frame lands on the stack, with points at seven in its own box.

02:07 The arrow doesn’t declare total or the team name, so the walk steps one box out, along its link. For this arrow, that’s the Know-It-Owls call’s box. Total goes from zero to seven, and the arrow returns the team’s name with its total.

02:22 Its frame comes off, and its points box goes too, because nothing links to it. Total stays at seven, because the arrow links to its box. Then the terminal shows Know-It-Owls: 7.

02:35 Line 11 calls scoreOwls() again, with nine. Your turn: pause the video and predict what line 11 logs.

02:48 It logs Know-It-Owls: 16. The arrow reached the same total, still at seven, and added nine. Once its frame comes off, the terminal shows 16.

03:01 A function together with the variables it links to from where it was made is called a closure. That’s how scoreOwls keeps its team’s total between calls.

03:11 Now Marco adds Table Six, on line 13. It’s the same function, but a new call. The call runs the body again, in a new box, so line 2 makes a new total, at zero.

03:24 It returns a new arrow, linked to that box, and its frame comes off. Now scoreTableSix holds the new arrow. Watch out: the two scorers don’t share one total.

03:36 Each total belongs to one call of makeTeamScorer(), not to the function itself.

03:42 Line 14 adds eight for Table Six, and the terminal shows Table Six: 8. Line 15 adds six for the Know-It-Owls. Their total was 16, so it shows 22.

03:57 So a function made inside a call keeps that call’s variables, even after the call returns. And each call makes new variables, so each scorer keeps a total of its own.

04:09 Next, in Build Scorers That Keep Their Own Totals, you’ll write scorers like these yourself, and hand them the settings they need.

Become a Member to join the conversation.