parent
73cdac8079
commit
c1c4842c11
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>30DaysOfJavaScript:19 Day </title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>30DaysOfJavaScript:19 Day</h1>
|
||||
<h2>Writing Clean Code</h2>
|
||||
|
||||
<script src="./data/countries_data.js"></script>
|
||||
<script src="./scripts/main.js"></script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
@ -0,0 +1,2 @@
|
||||
console.log(countries)
|
||||
alert('Open the console and check if the countries has been loaded')
|
@ -0,0 +1,104 @@
|
||||
<div align="center">
|
||||
<h1> 30 Days Of JavaScript</h1>
|
||||
<a class="header-badge" target="_blank" href="https://www.linkedin.com/in/asabeneh/">
|
||||
<img src="https://img.shields.io/badge/style--5eba00.svg?label=LinkedIn&logo=linkedin&style=social">
|
||||
</a>
|
||||
<a class="header-badge" target="_blank" href="https://twitter.com/Asabeneh">
|
||||
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
|
||||
</a>
|
||||
|
||||
<sub>Author:
|
||||
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
|
||||
<small> January, 2020</small>
|
||||
</sub>
|
||||
|
||||
</div>
|
||||
|
||||
[<< Day 18](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/18_Day/18_day_promise.md) | [Day 20 >>](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/20_Day/20_day_dom.md)
|
||||
|
||||
![Thirty Days Of JavaScript](../images/banners/day_1_19.png)
|
||||
- [Day 19](#day-19)
|
||||
- [Closure](#closure)
|
||||
- [Exercises](#exercises)
|
||||
- [Exercises: Level 1](#exercises-level-1)
|
||||
- [Exercises: Level 2](#exercises-level-2)
|
||||
- [Exercises: Level 3](#exercises-level-3)
|
||||
|
||||
# Day 19
|
||||
|
||||
## Closure
|
||||
|
||||
JavaScript allows writing function inside an outer function. We can write as many inner functions as we want. If inner function access the variables of outer function then it is called closure.
|
||||
|
||||
```js
|
||||
function outerFunction() {
|
||||
let count = 0;
|
||||
function innerFunction() {
|
||||
count++
|
||||
return count
|
||||
}
|
||||
|
||||
return innerFunction
|
||||
}
|
||||
const innerFunc = outerFunction()
|
||||
|
||||
console.log(innerFunc())
|
||||
console.log(innerFunc())
|
||||
console.log(innerFunc())
|
||||
```
|
||||
|
||||
```sh
|
||||
1
|
||||
2
|
||||
3
|
||||
```
|
||||
|
||||
Let us more example of inner functions
|
||||
|
||||
```js
|
||||
function outerFunction() {
|
||||
let count = 0;
|
||||
function plusOne() {
|
||||
count++
|
||||
return count
|
||||
}
|
||||
function minusOne() {
|
||||
count--
|
||||
return count
|
||||
}
|
||||
|
||||
return {
|
||||
plusOne:plusOne(),
|
||||
minusOne:minusOne()
|
||||
}
|
||||
}
|
||||
const innerFuncs = outerFunction()
|
||||
|
||||
console.log(innerFuncs.plusOne)
|
||||
console.log(innerFuncs.minusOne)
|
||||
```
|
||||
|
||||
```sh
|
||||
1
|
||||
1
|
||||
```
|
||||
|
||||
🌕 You are making progress. Maintain your momentum, keep the good work. Now do some exercises for your brain and for your muscle.
|
||||
|
||||
## Exercises
|
||||
|
||||
### Exercises: Level 1
|
||||
|
||||
1. Create a closure which has one inner function
|
||||
|
||||
### Exercises: Level 2
|
||||
|
||||
1. Create a closure which has three inner functions
|
||||
|
||||
### Exercises: Level 3
|
||||
|
||||
1. Create a personAccount out function. It has firstname, lastname, incomes, expenses inner variables. It has totalIncome, totalExpense, accountInfo,addIncome, addExpense and accountBalance inner functions. Incomes is a set of incomes and its description and expenses is also a set of expenses and its description.
|
||||
|
||||
🎉 CONGRATULATIONS ! 🎉
|
||||
|
||||
[<< Day 18](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/18_Day/18_day_promise.md) | [Day 20 >>](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/20_Day/20_day_dom.md)
|
Loading…
Reference in new issue