Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 9ac04da

Browse files
authored
Update LESSONPLAN.md
1 parent 707dee4 commit 9ac04da

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

Week3/LESSONPLAN.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,38 @@ Notes:
9898

9999
SECOND HALF (14.00 - 16.00)
100100

101-
## 4. The use and purpose of closures
101+
## 4. Closures
102102
### Explanation
103+
Credits to Yash:
104+
A closure is when inner function remembers the environment in which it was created even after the outer function has returned.
105+
106+
One powerful use of closures is to use the outer function as a factory for creating functions that are somehow related.
103107

108+
In the code snippet underneath we can see that the `carColor` function has still got access to the outer function's properties like `wheels`, `seats` and `brand` even after the function `manufactureCar` has returned. We can then use the `carColor` as a factory to create multiple cards of the same type but with a different color.
104109
### Example
110+
```JavaScript
111+
function manufactureCar() {
112+
const wheels = 4;
113+
const seats = 5;
114+
const brand = 'Some Brand';
115+
116+
return function carColor(color) {
117+
return {
118+
wheels,
119+
seats,
120+
brand,
121+
color,
122+
}
123+
};
124+
}
125+
126+
const basicCar = manufactureCar();
127+
128+
const redCar = basicCar('red');
129+
const blueCar = basicCar('blue');
130+
const greenCar = basicCar('green');
131+
```
105132

106-
Credits to Yash:
107133
``` Javascript
108134
{
109135
'use strict';
@@ -122,7 +148,9 @@ Credits to Yash:
122148
name('Yash Kapila');
123149
}
124150
```
151+
125152
### Exercise
153+
126154
### Essence
127155

128156

0 commit comments

Comments
 (0)