Javascript Interview Questions Flashcards
Can you name the two programming paradigms important for JavaScript app developers?
Prototypical inheritance (prototypes) and functional programming (closures, first class functions)
When is it good to use prototypical inheritance?
When you need to make multiple copies of object with the same methods. React components are good example.
What is functional programming?
It produces programs by composing math functions and avoids shared state and mutable data.
What are features that support functional programming?
first-class functions, higher-order functions and functions as arguments and values that can be passed around.
What is the difference between classical inheritance and prototypical inheritance?
Class: instances inherit from classes, and create sub-class relationships. Instances are instantiated using the 'new' keyword.Prototypical inheritance: instances inherit directly from other objects. Instances are typically created using Object.create(prototype).
Why are Javascript classes good?
They create tight coupling and provide encapsulation of the state.
Why are prototypes helpful?
They allow for easy inheritance and shared state between instances.
What are the pros of OOP?
It's easy to understand, and it's imperative so it reads like a straight-forward set of instructions.
What are the cons of OOP?
It depends on shared state. Objects and behaviors are tacked on the same entity, which can lead to side effects because any number of functions can access.
What's an example of a side effect with OOP or prototypical inheritance?
If two instances share the same prototype array and once changes the array, the second instance feels it.
What are the pros of FP?
Avoid shares state or side-effects. They use an imperative style, which concentrates on what to do, and not how. Computation that makes use of pure functions is also easy to scale across clusters.
What are the cons of FP?
The code is harder to read because things are abstracted away.
When is classical inheritance appropriate?
Never. Not even with React components. You should favor object composition over class inheritance.
With React, when should you use Functional Programming over classes?
If the components are 'dumb' and don't need to have state. They are stateless.
When is prototypical inheritance an appropriate choice?
In situations where modules or functional programming don't provide an obvious solution. When you need to compose objects from multiple sources or any time you need inheritance.
What kinds of prototypical inheritances is there?
delegation, concatenate, functional
What's an example of concatenative inheritance?
mixins, Object.assign()
What's an example of delegation inheritance?
prototype chain
What's an example of functional inheritance?
A function used to create a closure for private state.
What does it mean to enable composition?
Creating has-a, uses-a, or can-do relationships as opposed to is-a.
What does 'favor object composition over class inheritance' mean?
Make code more flexible. That code reuse should be achieved by assembling smaller units of functionality into new objects instead of inheriting from classes.
What are two-way data binding and one-way data flow, and how are they different?
Two-way means that UI fields are bound to model data such that when a UI field changes, the model data changes, and vice versa. One way data flow means that the model is the single source of truth. Changes in the UI trigger messages that signal user intent. Only the model has access to change the app's state.
What are the benefits of FP?
Modularity. Code reuse. Typed language.
What are programming side effects?
When a function changes a variable outside of its scope.
Why did OOP evolve?
Variables and side effects within the shared environment. Code became unpredictable.
What were the downfalls of introducing OOP to JS?
Methods rely on variables that are scoped to the parent. Methods that change variables outside of their scope. There are looping constructs. There are unnecessary variables.
Why is OOP good?
It allows for cleaner, more modular code, but there are still drawbacks.
Why did Functional JS evolve?
With JS' ability to treat functions as first-class objects, it can use pure functions that don't rely on the state of the code they're called from.
What's the benefit of functional programming?
The functions don't create side effects that alter variables outside of themselves. There is one and only one result a function returns. This makes it very easy to test and reuse.
What are the advantages of monolithic apps?
cross-cutting concerns, it's easy to hook up components and shared-memory access.
What are the cons to monolithic apps?
They get tightly coupled and entangled and can't evolve. They are also harder to maintain because of side effects and hidden magic.
What are the pros to microservice apps?
Better organized. Decoupled easier to recompose. Isolate and scale hot services.
What are the cons to microservice apps?
Can't have middleware to handle cross-cutting. Need to encapsulate another service later. They are also maintained on their own machines.
What's a good way to describe how microservices are built?
Each functional area of an app is implemented by its own microservice.
What does each microservice do?
It employs a backend API to talk to other services.
What is asynchronous programming, and why is it important?
Sync means that conditionals and function calls are executed sequentially from top-to-bottom, blocking on long tasks such as network requests. Async means that the engine runs in an event loop. When a blocking operation is needed, the request is started, and the code keeps running. When the response is ready, the callback is fired with the result.