Skip to content

Commit a373baf

Browse files
committed
Section 9: Function Expressions
1 parent 21c5beb commit a373baf

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

09-An-Advanced-Look-at-Functions/01/01.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ console.log(doubleArr([1, 2, 3]))
100100
*/
101101

102102
// ******************************
103-
// Lexical Scope******************************
103+
// Lexical Scope ******************************
104104
// Example 9
105-
105+
/*
106106
function outer() {
107107
let movie = 'Amadeus'
108108
@@ -178,3 +178,40 @@ function TodoList() {
178178
179179
}
180180
}
181+
*/
182+
183+
// ******************************
184+
// Function Expressions ******************************
185+
// Example 14
186+
console.log('\n')
187+
// this on it's own is not valid, we can't call this
188+
// We have no way of referring to this function
189+
// This is called an anonymous function
190+
// function(x, y) {
191+
// return x + y
192+
// }
193+
function add(x, y) {
194+
return x + y
195+
}
196+
197+
// Anonymous function expression
198+
const sum = function (x, y) {
199+
return x + y
200+
}
201+
202+
// Named function expression
203+
const product = function multiply(x, y) {
204+
return x * y
205+
}
206+
207+
// They work exactly the same way, but we declared them
208+
// in two different format, two different syntaxes
209+
console.log(add(1, 99))
210+
console.log(sum(1, 99))
211+
// console.log(multiply(1, 99)) // Error: multiply is not defined
212+
console.log(product(1, 99))
213+
214+
// Inspect the function
215+
console.dir(add)
216+
console.dir(sum)
217+
console.dir(product)

09-An-Advanced-Look-at-Functions/README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ if (true) {
9292

9393
**I cannot redeclared the same variable with the same name in the same scope**.
9494

95-
### Lexical Scope
95+
## 3. Lexical Scope
9696

9797
If i have a nested function like the one i have, here are the two functions i have outer and inside.
9898

@@ -110,7 +110,7 @@ function outer() {
110110
}
111111
```
112112

113-
But it's a one way relationship, It doesn't work the other way around
113+
**But it's a one way relationship, It doesn't work the other way around**
114114

115115
```
116116
function outer() {
@@ -127,6 +127,25 @@ function outer() {
127127
128128
outer() // hero is not defined
129129
```
130+
130131
The variable is successfully declared in inner, but i don't have access to it out here.
131132

132-
So a variable declared in one function is available to nested functions within it
133+
So a variable declared in one function is available to nested functions within it.
134+
135+
## 4. Function Expressions
136+
137+
There's another syntax we can use to define functions:
138+
139+
```
140+
const square = function (num) {
141+
return num * num;
142+
}
143+
square(7); // 49
144+
```
145+
146+
#### FUNCTIONS ARE... OBJECTS!
147+
In JavaScript functions are objects, which means we can put them in a variable, we can store 10 of them in an array, we can even pass them around as arguments, which is something we do all the time.
148+
149+
**There are some differences between function expressions and normal functions how these behave**
150+
151+
**You can add in a name for a function expression**

0 commit comments

Comments
 (0)