Use concise functions and captured state.
- arrow functions
- lexical this
- closures
- factory functions
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.
An arrow function is not always a drop-in replacement for a method that needs its own this.
Create a counter factory that returns increment and read functions; make two independent counters.
Open example.html in a browser and edit example.js. Save, refresh, and use the Console and debugger to inspect values.
- 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.