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
##How Passing Arguments Works (Values vs Reference)
96
+
# How Passing Arguments Works (Values vs Reference)
97
97
98
98
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.
99
99
@@ -134,3 +134,115 @@ checkIn(flight, eke);
134
134
```
135
135
136
136
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
+
constadd= (a, b) => a + b
148
+
149
+
constcounter= {
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
+
constgreet= () =>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
+
constgreet= () =>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
+
functioncount() {
192
+
let counter =0;
193
+
// Callback function
194
+
returnfunction () {
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
+
consttransformer=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
+
consthigh5=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.
0 commit comments