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
The only difference is that in arrow functions, the keyword `this` refers to the **parent scope**.
52
+
## Callback Functions
53
53
54
-
## Callback Functions
55
-
56
-
A **callback function** is a function that is passed into another function as a parameter. For example, setTimeout takes a callback function that will execute after 1000 ms, written using an arrow function or in the regular function notation:
54
+
A **callback function** is a function that is passed into another function as a parameter. For example, `setTimeout` takes a callback function that will execute after 1000 ms, written using an arrow function or in the regular function notation:
57
55
58
56
```js
59
57
// Snippet 1 - regular function notation
@@ -65,16 +63,38 @@ setTimeout(function() {
65
63
setTimeout(() => console.log("Hello!"), 1000);
66
64
```
67
65
68
-
When a function is passed into another function without being named, it is called an **anonymous function**. Both the `console.log` functions in the above snippets are anonymous functions. You'll see these a lot in javascript code, both in front end and back end development.
66
+
When a function is passed into another function without being named, it is called an **anonymous function**. Both the functions that `console.log` in the above snippets are anonymous functions. You'll see these *a lot* in javascript code, both in front end and back end development.
69
67
70
68
## Exercises
71
69
72
70
1. Write an arrow function that takes a string and returns the lowercase version of that string.
73
-
2. Write an arrow function that takes a string `prefix` and returns another function that prepends `prefix` to its parameter.
71
+
2. Write an arrow function that takes a string `prefix` and returns a **function** that prepends `prefix` to its own parameter.
72
+
73
+
```js
74
+
const prefix = "Hello, ";
75
+
const funcPrepend = (prefix) => {
76
+
// Your code here
77
+
}
78
+
// funcPrepend("hello") -> function that takes a string)
79
+
// funcPrepend("hello")("world") -> "hello world"
80
+
```
74
81
3. Write an arrow function that takes a string `prefix` and returns a **function** that takes a string `postfix` that returns another function that takes a string and returns correctly prepends/appends the pre/postfix to its parameter.
75
-
These practice questions are taken from [eloquentjavascript.net](https://eloquentjavascript.net/05_higher_order.html).
82
+
83
+
```js
84
+
const prefix = "Hello, ";
85
+
const postfix = "!";
86
+
const funcPrepend = (prefix) => {
87
+
// Your code here
88
+
}
89
+
// funcPrepend("hello") -> function that takes a string)
90
+
// funcPrepend("hello")("foo") -> function that takes a string
0 commit comments