Skip to content

Commit e3407f0

Browse files
committed
Improve closures concept with practical examples and implementation
1 parent 7ebb21d commit e3407f0

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

exercises/concept/coordinate-transformation/.docs/introduction.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Introduction
22

3-
**Closures** are a programming pattern in JavaScript which allows variables from an outer [lexical scope][wiki-lexical-scope] to be used inside of a nested block of code. JavaScript supports closures transparently, and they are often used without knowing what they are.
3+
**Closures** are a programming pattern in JavaScript which allows variables from an outer to be used inside of a nested block of code. JavaScript supports closures transparently, and they are often used without knowing what they are.
4+
5+
## Basic Closure Example
46

57
```javascript
68
// Top-level declarations are global-scope
@@ -19,7 +21,43 @@ function nDozen(n) {
1921
}
2022
```
2123

22-
## Closures to save state and pass along values
24+
## Advanced Closure Example: Function Factories
25+
26+
```javascript
27+
function createMultiplier(factor) {
28+
// `factor` is captured in the closure
29+
return function(number) {
30+
return number * factor;
31+
};
32+
}
33+
34+
const double = createMultiplier(2);
35+
console.log(double(5)); // 10 (uses closure-preserved `factor = 2`)
36+
```
37+
38+
## Practical Example: Coordinate Transformer
39+
40+
```javascript
41+
function createCoordinateTransformer(scaleX, scaleY) {
42+
// These parameters are preserved in the closure
43+
return function(x, y) {
44+
// This inner function uses both its arguments and the closure-preserved values
45+
return {
46+
x: x * scaleX,
47+
y: y * scaleY
48+
};
49+
};
50+
}
51+
52+
// Create specialized transformers
53+
const doubleScale = createCoordinateTransformer(2, 2);
54+
const graphicScale = createCoordinateTransformer(1.5, 3);
55+
56+
console.log(doubleScale(10, 20)); // {x: 20, y: 40}
57+
console.log(graphicScale(10, 20)); // {x: 15, y: 60}
58+
```
59+
60+
## Closures to Save State and Pass Along Values
2361

2462
Using a mutable variable declaration (like `let` or `var`) allows for some state to be preserved:
2563

@@ -34,4 +72,11 @@ export function increment() {
3472
}
3573
```
3674

37-
[wiki-lexical-scope]: https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scoping
75+
## Key Concepts to Remember
76+
77+
- Closures capture and preserve values from their outer scope.
78+
- Functions can return other functions, creating specialized tools.
79+
- Each closure maintains its own independent set of captured values.
80+
- Arguments can be used both when creating and calling closure functions.
81+
- Closures are excellent for creating reusable transformation functions.
82+
- The captured values remain accessible even after the outer function has finished executing.

0 commit comments

Comments
 (0)