You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit fda8472
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: Week4/Closures (class 10).md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,19 +17,19 @@ console.log(timirGreeting);
17
17
18
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.
19
19
20
-
When we call `greetPerson('Timir')`, the `greetPerson` function sets its `name` argument to the string `'Timir'` and then 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
+
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.
21
21
22
22
The returned the inner function is subsequently assigned to the variable `greetTimir`.
23
23
24
24
> Returning an inner function that accesses arguments and/or variables of its outer function creates (what we call) a **closure**.
25
25
26
-
With `greetTimir('Good morning')` we are now calling effectively calling the inner function, and passing it the string `'Good morning'`. Because the inner function still has acces to `name` it can produce the output printed by `console.log`:
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`:
27
27
28
28
```
29
29
Good morning, Timir
30
30
```
31
31
32
-
Alternatively, we could obtain the same result by scrapping all lines except the last two and replace them with the code below. We wouldn't be able to tell the difference from just looking at the output produced.
32
+
Alternatively, we could have obtained the same result by scrapping all lines except the last two and replace them with the code below. We wouldn't be able to tell the difference from just looking at the output produced.
However, with that alternative `greetTimir` function we can only greet Timir. In contrast, with the `greetPerson` function we can easily produce functions to greet any person we like (or don't like :wink:), e.g.:
44
+
However, with that alternative `greetTimir` function we can only greet Timir. In contrast, with `greetPerson` we can easily produce functions to greet any person we like (or don't like :wink:), e.g.:
0 commit comments