Skip to content

Commit 31c9851

Browse files
committed
complete function returning functions
1 parent 8bb7488 commit 31c9851

File tree

2 files changed

+181
-1
lines changed

2 files changed

+181
-1
lines changed

10-A_Closer_Look_At_Functions/README.md

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ In a situation we want to skip a parameter, we can simply assign it `undefined`,
9393
createBooking("LH123", undefined, 1000); // numPassengers = 1, price = 1000
9494
```
9595

96-
## How Passing Arguments Works (Values vs Reference)
96+
# How Passing Arguments Works (Values vs Reference)
9797

9898
Passing a primitive type to a function is the same as creating a copy outside of the function, so the value is simply coppied. On the other hand when we pass an object to a function, it is just like copying the object, so whatever we change in the copy will also affect the original.
9999

@@ -134,3 +134,115 @@ checkIn(flight, eke);
134134
```
135135

136136
In programming there are two terms used all the time when dealing with functions: `passing by value` and `passing by reference`. JavaScript does not have `passing by reference`, only `passing by value`.
137+
138+
# First Class and Higher Order Functions
139+
140+
First class functions are functions that are treated like any other variable or values. For example, this function can be passed as an argument to other functions. It can also be returned by another function or assigned as a value to a variable.
141+
142+
This of course works this way because functions are essentially just objects
143+
144+
Example
145+
146+
```js
147+
const add = (a, b) => a + b
148+
149+
const counter = {
150+
value: 23,
151+
inc: function({this.value++}),
152+
}
153+
```
154+
155+
In the example above, we created a function expression in the first example and an object method in the second example.
156+
157+
We can also pass functions as arguments into other functions.
158+
159+
```js
160+
const greet = () => console.log("Hey Eke");
161+
btnClose.addEventListener("click", greet);
162+
```
163+
164+
We passed the greet function inside the `addEventListener` function as an argument.
165+
166+
- We can also return functions from other functions
167+
- Just like array methods, functions also have built-in methods as well. For example the `bind()` method.
168+
169+
```js
170+
counter.inc.bind(someObject);
171+
```
172+
173+
## Higher Order Function
174+
175+
These are simply fuctions that receives another function as an argument, that returns a new function or both.
176+
177+
This is only possible because of first class functions.
178+
A great example is the `addEventListener()` function we saw earlier.
179+
180+
```js
181+
const greet = () => console.log("Hey Eke");
182+
btnClose.addEventListener("click", greet);
183+
```
184+
185+
In this example, the `addEventListener()` function is the higher order function and this because it receives another function as an input. While the function that is passed in is called a callback function, and this is because the callback function will be called later by the higher order function.
186+
187+
Finally we can also have functions return another function.
188+
189+
```js
190+
// Higher order function
191+
function count() {
192+
let counter = 0;
193+
// Callback function
194+
return function () {
195+
counter++;
196+
};
197+
}
198+
```
199+
200+
# Functions Accepting Callback Functions
201+
202+
Callbacks functions are essential for splitting up code into smaller reusable parts.
203+
204+
- Most importantly, they allow us to create abstractions. Which simply mean we hide secondary code implementation because so as to focus on the important parts of the code.
205+
206+
This allows us to think about problems on a higher, more abstract level.
207+
208+
```js
209+
const transformer = function (str, fn) {
210+
console.log(`Original string: ${str}`);
211+
console.log(`Transformed string: ${fn(str)}`);
212+
213+
console.log(`Transformed by: ${fn.name}`);
214+
};
215+
216+
transformer("JavaScript is the best!", upperFirstWord);
217+
transformer("JavaScript is the best!", oneWord);
218+
219+
// JavaScript uses callbacks all the time.
220+
const high5 = function () {
221+
console.log("👋🏽");
222+
};
223+
224+
["Victor", "Martha", "Adam"].forEach(high5);
225+
document.body.addEventListener("click", high5);
226+
```
227+
228+
In the example above, `transformer()` function delegates the string transformation task to the lower level functions.
229+
230+
# Functions Returning Functions
231+
232+
```js
233+
const greet = function (greetMsg) {
234+
return function (name) {
235+
console.log(`${greetMsg} ${name}`);
236+
};
237+
};
238+
// Arrow function
239+
const greetAr = (greetMsg) => (name) => console.log(`${greetMsg} ${name}`);
240+
241+
const gretterHey = greet("Hey");
242+
gretterHey("Victor");
243+
gretterHey("Ben");
244+
245+
greet("Hello")("Caleb");
246+
```
247+
248+
# The Call and Apply Method

10-A_Closer_Look_At_Functions/script.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,71 @@ const newPassport = function (person) {
5353

5454
newPassport(eke);
5555
checkIn(flight, eke);
56+
57+
// 🔸 First Class Function vs Higher Order Functions 🔸
58+
const add = (a, b) => a + b;
59+
60+
const counter = {
61+
value: 23,
62+
inc: function () {
63+
this.value++;
64+
},
65+
};
66+
67+
// const greet = () => console.log("Hey Eke");
68+
// btnClose.addEventListener("click", greet);
69+
70+
// Higher order function
71+
function count() {
72+
let counter = 0;
73+
// Callback function
74+
return function () {
75+
counter++;
76+
};
77+
}
78+
79+
// 🔸 Functions Accepting Callback Functions 🔸
80+
const oneWord = function (str) {
81+
return str.replaceAll(" ", "").toLowerCase();
82+
};
83+
84+
const upperFirstWord = function (str) {
85+
const [first, ...others] = str.split(" ");
86+
return [first.toUpperCase(), ...others].join(" ");
87+
};
88+
89+
// Higher-order function
90+
const transformer = function (str, fn) {
91+
console.log(`Original string: ${str}`);
92+
console.log(`Transformed string: ${fn(str)}`);
93+
94+
console.log(`Transformed by: ${fn.name}`);
95+
};
96+
97+
transformer("JavaScript is the best!", upperFirstWord);
98+
transformer("JavaScript is the best!", oneWord);
99+
100+
// JavaScript uses callbacks all the time.
101+
const high5 = function () {
102+
console.log("👋🏽");
103+
};
104+
105+
["Victor", "Martha", "Adam"].forEach(high5);
106+
document.body.addEventListener("click", high5);
107+
108+
// 🔸 Functions Returning Functions 🔸
109+
const greet = function (greetMsg) {
110+
return function (name) {
111+
console.log(`${greetMsg} ${name}`);
112+
};
113+
};
114+
// Arrow function
115+
const greetAr = (greetMsg) => (name) => console.log(`${greetMsg} ${name}`);
116+
117+
const gretterHey = greet("Hey");
118+
gretterHey("Victor");
119+
gretterHey("Ben");
120+
121+
greet("Hello")("Caleb");
122+
123+
// 🔸 The Call and Apply Method 🔸

0 commit comments

Comments
 (0)