Skip to content

Latest commit

 

History

History
37 lines (24 loc) · 1.01 KB

File metadata and controls

37 lines (24 loc) · 1.01 KB

14. Arrow functions and closures

Use concise functions and captured state.

Learn

  • arrow functions
  • lexical this
  • closures
  • factory functions

How it works

An arrow function is a compact function expression and does not create its own this binding. A closure retains access to variables from the surrounding scope after that outer call has returned. Factories can use this to keep private state.

Common mistake

An arrow function is not always a drop-in replacement for a method that needs its own this.

Guided challenge

Create a counter factory that returns increment and read functions; make two independent counters.

Run the example

Open example.html in a browser and edit example.js. Save, refresh, and use the Console and debugger to inspect values.

Completion check

  • Explain the core idea in your own words without reading the code.
  • Change one input and predict the output before running it.
  • Test one boundary or failure case, then record what you changed.