Callback function
: a function that is passed into another function as a parameter then invoked by that other function
(usecases: Advanced Array Methods, Broswer events, AJAX Requests, React Development)
A higher order function
: a function that accept a callback as a parameter
Use a callback function to make your code more general
Create callbacks using anonymous functions
Foreach function
: function forEach(array, callback){
for (var i = 0; i < arr.length; i++) {
callback(arr[i], i, arr);
}
}
var arr = [1,2,3,4,5,6];
function double(arr) { function forEach(arr, function(number){
for(var i = 0; i < arr.length; i++) { console.log(number * 2);
console.log(arr[i] * 2); } });
}
double(arr);
findIndex
: Returns the index of the first element in the array for which the callback returns truthy value.
-1 is returned if the callback never returns a truthy value.
function findIndex(arr, callback) {
for (var i = 0; i < arr.length; i++) {
if(callback(arr[i], i, arr)) {
return i;
}
}
return -1;
}
The Stack and The Heap
Stack : An ordered data structure, Keeps track of function invocations, Part of the JavaScript runtime
How Your Code Changes the Stack?
Whenever you invoke a function, the details of the invocation are saved to the top of the stack(pushed to the top)
Whenever a function returns, the information about the invocation is taken off the top of the stack(popped off of the top)
Heap: An area in memory where the your data is stored
Ex. function upperCaseFirst(word) {
return word[0].toUpperCase() + word.slice(1);
}
function upperCaseWords(sentence){
var words = sentence.split(" ");
for(var i = 0; i < words.length; i++) {
words[i] = upperCaseFirst(words[i]);
}
return words.join(" ");
}
upperCaseWords("lowercase words");
The Queue
: An ordered list of functions waiting to be placed on the stack
Functions in the queue are processed on a first in, first out basis(FIFO)
The Event Loop
: Functionality in the JavaScript runtime that checks the queue when the stack is empty
If the stack is empty, the front of the queue is placed in the stack
JavaScript is Single Threaded
'Udemy > Web Dev BootCamp' 카테고리의 다른 글
Advanced Array Methods(map) (0) | 2019.02.19 |
---|---|
Advanced Array Methods(forEach) (0) | 2019.02.19 |
4 Ways AJAX(xhr / fetch / jquery / axios) (0) | 2019.02.18 |
Fetch Random User Profile Generator 코드리뷰 (0) | 2019.02.17 |
AJAX (0) | 2019.02.15 |