Advanced Javascript Flashcards
What is a closure?
A function bundled with its lexical environment. (Variables in scope.)
When are closures created?
Function creation time.
What is a promise?
An object which represents a potential future value or error (reason that it can’t resolve).
What is the purpose of a promise?
Solves the callback function pyramid of doom.
What is functional programming?
Programming using pure functions as the atomic unit of composition, avoiding shared mutable state, and side-effects. Higher order functions for abstractions, favoring declarative style over imperative.
What is a pure function?
Given same arguments => same output, no side effects. A.k.a deterministic.
Is Math.max() a pure function?
Yes, no side effect.
Is localStorage.setItem() a pure function?
No, has the side effect of storing information to browser.
Is Math.random() a pure function?
No, not deterministic (different output with same input).
Is console.log() a pure function?
No, changes the state of the console, which is observable outside the function
Is throw new Error(‘Foo’) a pure function?
No, has the side effect of throwing an error (unless you’re inside a promise, then it will just reject)
What is the issue with 2-way data binding?
There’s more than one place responsible for changes, making it harder to understand how things changed and how the application state got into the state it’s in.
Why would someone choose React?
- Deterministic view rendering.
- 1-way data flow. Props and component state are immutable. While the component is rendering, props and state CAN’T change.
- React components are just functions. If you know JavaScript and HTML, congrats! You’re 90% done learning React.
- React does not use templates. JSX is a syntax extension to JavaScript - just use JavaScript map, filter, for, if, etc…
- Synthetic events - event pools, automatic event delegation
What is the purpose of React Hooks?
Provides high-level access to React’s low-level component lifecycle capabilities. (useState, useReducer, useEffect, useRef) - great for making code more modular, separating concerns, etc.
What is point-free style?
Writing functions without mention of the arguments.
What is a curried function?
A function that takes multiple arguments by taking one argument per invocation. Takes an argument and returns a function until no arguments are left, then returns the result.
What is a partial application?
A function which has already been applied to some, but not yet all of its arguments.
What is function composition?
Function composition is the process of applying a function to the return value of another function.
What is encapsulation?
The bundling of data and the methods that act on that data such that access to that data is restricted from outside the bundle. In OOP, that means that an object stores its state privately, and only the object’s methods have access to change it.
What is the problem with shared mutable state?
If your input state depends on some other instruction’s output state, and any sort of concurrency arises, it creates race conditions. If you change the order in which instructions are called, it can alter the results. Mix in any sort of non-determinism in sequencing, and the result is chaos: unpredictable, unprovable, seemingly random application state. Sometimes it works. Sometimes it doesn’t.