Skip to content

Commit f6ae327

Browse files
committed
text tweaks
1 parent 0949b99 commit f6ae327

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

‎Week4/Closures (class 10).md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Closures
22

3-
Below is a stripped down, simplified version of the closure example we used during the lecture.
3+
Below is a stripped down, simplified version of the first closure example we used during the lecture.
44

55
```js
66
function greetPerson(name) {
@@ -15,20 +15,24 @@ const timirGreeting = greetTimir('Good morning');
1515
console.log(timirGreeting);
1616
```
1717

18-
The `greetPerson` function (the "outer" function) contains another function inside its function body, called `innerFunction`. This inner function has access to the `name` argument of `greetPerson`, as well as its own `greeting` argument.
18+
## Description
1919

20-
When we later call `greetPerson('Timir')`, the `greetPerson` function sets its `name` argument to the string `'Timir'` and returns its inner function. This inner function **retains access to the `name` argument** and its value `'Timir'`, even after `greetPerson` exits. (Note that `greetPerson` doesn't call its inner function; it just returns it.)
20+
The `greetPerson` function (the "outer" function) contains an enclosed function, named `innerFunction`. This inner function has access to the `name` argument of its enclosing function `greetPerson`, as well as to its own `greeting` argument.
2121

22-
The returned the inner function is subsequently assigned to the variable `greetTimir`.
22+
When we later call `greetPerson` passing `'Timir'` as its parameter, the `greetPerson` function sets its `name` argument to `'Timir'` and returns its inner function. This inner function **retains access to the `name` argument** and its value `'Timir'`, even after `greetPerson` exits. (Note that `greetPerson` doesn't call its inner function; it just returns it.)
23+
24+
The returned inner function is subsequently assigned to the variable `greetTimir`.
2325

2426
> A closure is an inner function that has access to the outer (enclosing) function’s variables and arguments.
2527
26-
With `greetTimir('Good morning')` we are effectively calling the inner function, passing it the string `'Good morning'`. Because the inner function still has acces to `name` it can produce the output as printed by the `console.log`:
28+
With `greetTimir('Good morning')` we are effectively calling the inner function and passing it the string `'Good morning'`. Because the inner function still has access to `name` it can produce the output as printed by the `console.log`:
2729

2830
```
2931
Good morning, Timir
3032
```
3133

34+
## Alternative implementation
35+
3236
We could have obtained the same result by scrapping all lines except the last two and replace them with an equivalent, custom made `greetTimir` function as shown below. We wouldn't be able to tell the difference from just looking at the output produced.
3337

3438
```js
@@ -53,7 +57,7 @@ const hasanGreeting = greetHasan('Have a good day');
5357
console.log(hasanGreeting);
5458
```
5559

56-
### Recommended further reading
60+
## Recommended further reading
5761

5862
- [Eloquent JavaScript - Chapter 3: Functions](http://eloquentjavascript.net/03_functions.html)
5963
(Press Ctrl+F in your browser and search for the word 'closure')

0 commit comments

Comments
 (0)