W7D2 Flashcards

1
Q

What are higher order functions?

A

They can define and return function or accept callbacks as arguments. They can also do both.

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

What are callbacks?

A

They are functions that are passed as arguments to higher order functions

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

What are Closures?

A

Closures are functions that use variables that are defined outside of its scope.

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

What are free variables?

A

Free Variables are variables defined outside of its scope.

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

What type of scope is closures?

A

Closures use lexical scoping

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

What are composing functions?

A

Composing Functions use currying. The outermost function receives an initial value, but has a nested composing function. This composing function take 2 functions that do something to the initial value that was received.

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

Wrote a composing function?

A
const compose = function (f, g) {
  return function (x) {
    return f(g(x));
  };
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is middleware?

A

Middleware is software that intercepts a process either to redirect it or generate some side effect.

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

How is middleware initiated?

A

When a dispatch is made.

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

How does middleware work?

A

Middleware works when a dispatch is made, the middleware intercepts the action, before it reaches the reducer. When the action is intercepted, middleware will test if the action is a function. If so, it will invoke the action, otherwise it will pass the action along.

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

What are the 4 things middleware can do?

A
  1. It can resolve the action
  2. It can pass along the action if it is not a function
  3. Generate a side effect
  4. send another dispatch
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the 3 arguments that createStore takes? Say it in the correct order

A
  1. reducer
  2. preloadedState
  3. enhancer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the logger middleware?

A

Logger prints the store’s state before and after the action is processed.

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

What is a function signature?

A

A function signature is the set of inputs and outputs of a function.

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

What is the function signature of middleware? And why is important for middleware conform to a particular function signature?

A

Every middleware must receive the ‘store’ as an argument and returns a function that takes the ‘next link’ in the middleware chain as an argument. The nested function with the argument of the next link returns another function that receives the ‘action’ as an arguments. In the final function, it will either trigger any side effects before returning the next(action).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
Write the middleware function signature. 
const middleware = ...
A
const middleware = store => next => action => {
  const result  = next(action);
  return result
};
17
Q

What are all the things that the logger middleware can do?

A

It can receive the store as its only argument.
It can return a function that receives the next middleware.
It also return a function receiving the action.

18
Q

Write the logger middleware’s function signature.

A

const logger = state => next => action => {

console. log(‘Action received: ‘, action);
console. log(‘State pre-dispatch:’, store.getState());

let result = next(action);

console.log(‘State post-dispatch:’ ,store.getState());

return result;

}

19
Q

What do want the source to every change to our app state be?

A

Action creator

20
Q

What is the purpose of thunk middleware?

A

Thunk middleware allows you to write action creators that return a function instead of an action. Thunks can be used to delay the dispatch of an action or to dispatch only if a certain condition is met.

21
Q

Write thunk function source code

A
function createThunkMiddleware(extraArg) {
  return({dispatch, getState}) => next => action{
    if(typeof(action === 'function') {
      return action(dispatch, getState, extraArg);
    }
   return next(action)
  }
}
const thunk = createThinkMiddleware();
thunk.withExtraArg = createThunMiddleware;

export default thunk;

22
Q

What part of the store does thunk middleware receive?

A

dispatch and getState

23
Q

Write thunk function

A
const thunk = ({ dispatch, getState }) => next => action => {
  if (typeof action === 'function') {
    return action(dispatch, getState);
  }
  return next(action);
};

export default thunk;

24
Q

Why do pass getState function to thunk?

A

In case our async action creators need access to our application state.

25
Q

What does the reducer doe if the state changes?

A

It must return a new state object without mutating the original state.

26
Q

How do you prevent the mutation of the state?

A

By using Object.freeze

27
Q

What does Object.freeze do?

A

It prevents new properties from being added to an object and also prevents properties currently on an object from being altered or deleted.

28
Q

What is the weakness to Object.freeze?

A

It is not a deep freeze. Object.freeze can still mutate nested objects.

29
Q

What is happening in the code snippets A and B?

Code A:
people.farmers = { name: ‘Young MacDonald’ };
people //=> { farmers: {name: ‘Old MacDonald’ } }

Code B:
people.farmers.name = ‘Young MacDonald’;
people //=> { farmers: {name: ‘Young MacDonald’ } }

A

In Code A, farmers is not a nested object, thus Object.freeze will prevent mutation on it. However in Code B, the name key in farmers is a deeply nested object, thus Object.freeze cannot prevent it from being mutated.

30
Q

What is a namespace?

A

A namespace is just a subset of controllers that live under a specific URL.

31
Q

What is happening below?

namespace :api do
resources :cats, only: [:index]
end

A

In the controllers folder in our rails project, we have a nested directory called api. In api, we now put a subset of controllers in the api directory. In the api directory the cats controller is not a part of the subset of the api’s controllers.

In the code, we are telling the router to look for the cats controller in the api directory.