Repository navigation
Expand file tree
/
Copy pathnotes.txt
More file actions
54 lines (44 loc) · 2.36 KB
/
Copy pathnotes.txt
File metadata and controls
54 lines (44 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Notes Section
Callbacks Lambda Exlplanation:
- callback functions are functions passed into other functions as arguments
- example 1: const functionFeeder = function(callback) {
callback('Hello from the inside of Function Feeder');
};
- here, we defined functionFeeder as the function and gave it a paramter called "callback"
- this means that when we invoke functionFeeder we can give it another function as an argument (that's why between the curly brackets
it says "callback")
- in this example the function that we call will feed the string 'Hello from the inside' to the callback function we want to use
- example 1 extended: functionFeeder((string) => { // invoking the function
alert(string); // alert a function that pops up a box in the browser.
});
- in this situation we are telling functionFeeder to alert (this is also a Javascript command!) the string we give it
- the "functionFeeder(string)" is what we use to invoke the function
- example 2: function sayHello(name) {
console.log(`Hello, ${name}`);
}
- because we want to use this function over and over again, it is appropriate to use the callback method
- example 2 extended: function callSayHelloWithLars(callback) {
const innerName = 'Lars';
callback(innerName);
}
- here we made a new function that allowed us to use the first function again
- I'm not sure why the callback is in parentheses... is this just for the example or a coding standard?
- example 2 extended: callSayHelloWithLars(sayHello);
- to finish, we need to invoke the function so it can perform the callback
Example 3 Lambda:
- setup: const elements = ['earth', 'wind', 'fire', 'water'];
- "create a function that passes back the first item in an array":
function showFirst(array, callback) {
callback(array[0]);
}
showFirst(elements, (firstItem) => {
alert(firstItem);
});
- is this it? how do I call the function? what goes in the paramters?
** Taking notes here is ineffective. Will post links I used instead. **
LINKS:
- Callbacks in general: https://codeburst.io/javascript-what-the-heck-is-a-callback-aba4da2deced
- Challenge 5 new concept: indexOf
- https://stackoverflow.com/questions/14461450/check-if-string-inside-an-array-javascript
- https://appdividend.com/2018/12/19/javascript-array-indexof-example-tutorial/
- https://www.w3schools.com/jsref/jsref_indexof_array.asp