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

Scope, Closures and the Call Stack (Summary)

You’ve finished Scope, Closures and the Call Stack, and Marco has his scorekeeping kit for quiz night at the Lantern. Two pictures sit behind everything it does. The first is the call stack. Each call puts a frame on top, and the caller waits under it. A return takes off only the top frame and hands its value down. When roundTotal() calls itself, each category gets a frame of its own.

The second is the box a scorer keeps. makeTeamScorer() returns an object of three functions, and its frame comes off. All three functions still link to that call’s variables, so JavaScript keeps them. That’s a closure. The Know-It-Owls’ total starts at 0, and their .add() calls together take it to 30. Then the pipeline ranks the teams, and the pub hears the winner once.

In This Course, You Learned How To

  • Follow how nested and recursive calls run
  • Find the declaration a name refers to, from where the code is written
  • Build functions that remember state between calls
  • Compose small functions into pipelines
  • Build Marco’s scorekeeping kit, and explain how it works

Things to Remember

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 finished Scope, Closures and the Call Stack. In this course, you learned to follow how nested and recursive calls run. To find the declaration a name refers to, from where the code is written.

00:13 To build functions that remember state between calls. To compose small functions into pipelines. And you built Marco’s scorekeeping kit, and explained how it works.

00:26 Two pictures sit behind Marco’s kit. First, the call stack. Each call puts a frame on top, and the caller waits under it. A return takes off only the top frame, and hands its value down.

00:39 When roundTotal() calls itself, each category gets a frame of its own.

00:45 Second, the box a scorer keeps. makeTeamScorer() returns an object of three functions, and its frame comes off. They still link to that call’s variables, so JavaScript keeps them.

00:57 That’s a closure. The Know-It-Owls’ total starts at zero, and their .add() calls together take it to thirty.

01:05 The pipeline ranks the teams, and the pub hears the winner once.

01:11 Things to remember. Read an error’s stack trace top first: where it broke, then how it got there. Look a name up where the code is written, not where it’s called.

01:24 A closure links to a variable, not a copy. Each call of a factory makes new variables. A cache key needs every argument. And a wrapper passes every argument on and returns the result.

01:40 The RealJS glossary has pages on the call stack, closures, and higher-order functions.

01:47 Thanks for watching.

Become a Member to join the conversation.