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

Make a Function That Remembers

In this video, you answered the egg timer’s question and saw how a function made inside another one still has what it needs after that other function has finished.

Key Points

  • A call that’s over can leave its box behind. Each call of a function makes a box for its parameters and variables. Usually, once the call is over, nothing links to that box. Your code can never reach it again, and JavaScript clears it away.
  • A function made inside a call links to that call’s box. If the call returns that function, or hands it to setTimeout(), the link keeps the box alive, and the function can still read the box’s variables whenever it runs.
  • A call’s variables don’t become top-level names. Typing factor at the top level gives a ReferenceError, because a walk from the top level never looks inside a function’s box. Only a function made inside the box can reach them, by walking one box out.
  • Every call makes a new box. Where a function is written decides what its box holds and which boxes surround it, and each call makes a fresh box of that shape. Calling makeScaler() twice made two boxes, each with its own factor, and each returned arrow links to the box of the call that made it. Making the second scaler didn’t change the first.
  • A function together with the box it keeps is called a closure. The egg timer’s arrow is one too. Each call of startEggTimer() makes its own box, so each timer logs its own pan.

The Code

Open a new tab at about:blank, open the Console, and type these entries. A scaler for Grandma Ines’s lentil soup, which serves six and uses 200 grams of lentils:

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 Follow the Scope Chain ended on an egg timer with a question left open. In this video, you’ll see how a function made inside another one keeps what it needs, even after that other function has finished.

00:13 Open a new tab at about:blank and open its Console. Type along, and keep that tab open to the end. Here’s that timer again, as one entry, with Shift+Enter between the lines.

00:25 The function startEggTimer takes a pan. Inside it, setTimeout gets an arrow that logs the pan a second later. Then the function logs that the timer is set.

00:36 The entry ends by calling it with the small pan. Press Enter, and Timer set for the small pan appears at once. That log is the last line of startEggTimer, so the call is over.

00:48 A second later, Eggs ready in the small pan appears. The arrow still found the pan, in a call that had already finished.

00:58 With a timer, the browser decides when the arrow runs. So here’s a function made inside a call that you can run yourself, whenever you like. Sam cooks Grandma Ines’s lentil soup for more or fewer people than its six servings, so Sam’s app needs to scale the amounts.

01:16 The function makeScaler takes a factor. It returns an arrow that multiplies an amount by that factor. So makeScaler doesn’t scale anything itself. It hands back a function that will.

01:30 Call makeScaler with two, and store what it returns in forTwelve, for twelve servings. Each call of makeScaler makes a fresh box, sitting where makeScaler is written.

01:41 This one has factor set to two. The arrow is made inside that box, so it links to that box. Then the call returns the arrow, and it’s over.

01:54 Usually, when a call is over, nothing links to its box. Your code can never reach it again, and JavaScript clears it away. Here, the arrow in forTwelve still links to the box, so the box stays.

02:08 That doesn’t make factor a top-level name. You can confirm this: type factor on its own. The Console shows a ReferenceError, factor is not defined. A walk from the top level never looks inside a function’s box.

02:24 Now call forTwelve with the soup’s 200 grams of lentils. It shows 400. The arrow’s walk for factor went one box out, to the box that stayed, and found two there.

02:37 Sam cooks the soup for three people too, which is half. Call makeScaler again, and this time store its arrow in forThree, with a factor of 0.5. For the lentils, forThree shows 100.

02:52 Now forTwelve and forThree both came from makeScaler. Your turn: pause the video and predict what forTwelve shows for 200 grams now.

03:07 It still shows 400.

03:10 The second call made a second box, with its own factor. It didn’t touch the first box. Each arrow links to the box of the call that made it. So the rule is that a function made inside a call keeps that call’s box after the call is over.

03:27 Every call makes a new box. A function together with the box it keeps is called a closure. That answers the egg timer’s question. Its arrow is a closure, too.

03:38 Start two timers in one entry, with Shift+Enter between the two calls, for the small pan and the big pan. Both lines say a timer is set. A second later, each arrow logs its own pan, because each call made its own box.

03:55 That’s how each step of Sam’s recipes can get a timer that remembers its own step. Next, in Keep State Private With Closures, you’ll give a recipe a step counter that only its own functions can change.

Become a Member to join the conversation.