Javascript Flashcards

1
Q

Async Evolution

A

callbacks (unmanageable callback hell) → promises with then and catch → async/await (write code normally, no long promise chaining)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Callback

A

A function that is passed as an argument. The function will be executed after a certain operation is finished. This is useful for handling async operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Promise

A

An alternative approach for callbacks for handling async operations. It allows you to set an operation to run after another operation finish. It is a syntactic improvement that prevents unclean callback hell.

a promise returns resolve or reject

handled with then and catch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

async / await keyword

A

Syntactic sugar for handling promise, without then and catch

const res = await someFunc()
console.log(res)

make sure res is printed only after someFunc resolved

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Basic of async

A

Blocking codes: expensive task or intensive chunk of code

When executed without returning control to the browser, it will appear frozen

Javascript main thread is single-threaded

A thread can only execute a task at a time

expensive operations: I/O operations like HTTP requests, disk reads and writes or database operations

when encountering an async I/O operations it will be assigned to a kernel thread (system kernel are multi-threaded)

with promise or callback, the I/O operation will not block the main thread

after operation is finished, callback is being put into a callback queue in the event loop

once it can be safely handled it will be pushed to the JS call stack, to be called by the main thread

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
for (let i = 0; i < 5; i++) {
  setTimeout(function() { console.log(i); }, i * 1000 );
}
for (var i = 0; i < 5; i++) {
	setTimeout(function() { console.log(i); }, i * 1000 );
}
A

setTimeout(function() { console.log(i); }, i * 1000 );
setTimeout(function() { console.log(i); }, i * 1000 );
setTimeout(function() { console.log(i); }, i * 1000 );
setTimeout(function() { console.log(i); }, i * 1000 );
setTimeout(function() { console.log(i); }, i * 1000 );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Bubbling

A

When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Capturing

A

event goes down to the element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

event target vs current target

A
  • event.target– is the “target” element that initiated the event, it doesn’t change through the bubbling process.
  • this– is the “current” element, the one that has a currently running handler on it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to stop Bubbling

A

event.stopPropagation()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Capturing and Bubbling Phase

A
  1. Capturing phase – the event goes down to the element.
  2. Target phase – the event reached the target element.
  3. Bubbling phase – the event bubbles up from the element
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Arrow Function vs Regular Function

A
  • Structure
  • This
  • Implicit Returns
    If the arrow function contains one expression, without curly braces, then the expression is implicitly returned. These are the inline arrows function.
  • Constructor
    can’t be used as constructor
  • Argument
    arguments object equals to the arguments of the outer function, can use …args to get direct arguments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Arrow vs Regular “this”

A
> `this`value inside of an arrow function always equals`this` value from the outer function. In other words, the arrow function resolves`this` lexically.
> no more`const self = this` or`callback.bind(this)`workarounds.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Debounce vs Throttle

A

Debouncing forces a function to wait a certain amount of time before running again. The debounced function will ignore all calls to it until the calls have stopped for a specified time period. Only then will it call the original function. (e.g. autosave feature)

To throttle a function means to ensure that the function is called at most once in a specified time period (for instance, once every 10 seconds) (e.g. limiting API hit)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Closure

A

A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

DOM Manipulation

A

Document Object Mode, organizes the elements on a web page and allows scripting languages to access them

DOM Manipulation in JS is costly

adding a first child, will rebuilt the whole list

17
Q

React Virtual DOM

A

In React, for every DOM object, there is a corresponding “virtual DOM object.”

Manipulating the virtual DOM is much faster, because nothing gets drawn onscreen

Render process

  1. The entire virtual DOM gets updated.
  2. The virtual DOM gets compared to what it looked like before you updated it. React figures out which objects have changed.
  3. The changed objects, and the changed objects only, get updated on the real DOM.
  4. Changes on the real DOM cause the screen to change.

add example about using key in lists

18
Q

React functional updates on state hooks

A

onClick={() => {
setCount(count + 1)
setCount(count + 1)
}}

will only increment count once

onClick={() => {
setCount((previousCount) => previousCount + 1)
setCount((previousCount) => previousCount + 1)
}}

increment count twice

It will bail out of rendering if you try to update to the same value that your state is currently holding.