javascript reading Flashcards

1
Q

Can you name two programming paradigms important to Javascript Application Developers?

A
  • Prototypal Inheritance (also: protoypes, OLOO - Objects Linking to Other Objects)
  • Functional Programming (also: closures, first class functions, lambdas)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why are constructors messy in Javascript?

A

Constructors violate the open/closed principle because they couple all callers to the details of how your object gets instantiated.

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

What is a Factory Function?

A

Factory Functions are simply constructors minus the ‘new’ requirement, global pollution danger, and awkward limitations.

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

Does Javascript need constructor functions?

A

NO! Javascript does not need constructor functions because any function can return a new object. Javascript has dynamic object extensions, object literals, and ‘Object.create()’. ‘this’ will also behave like it does in any other function.

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

Name some Classical Inheritance Issues.

A

Tight Coupling - The coupling between a child class and its parent is the tightest form of coupling in OO design. That’s the opposite of reusable, modular code. Making small changes to a class creates rippling side-effects that break things that should be completely unrelated

  • Class of Tool
  • Class of Weapon
  • Class of Clue (Game)

Duplication by Necessity Problem - The obvious solution to taxonomy troubles is to go back in time, build up new classes with subtle differences by changing up what inherits from what — but it’s too tightly coupled to properly extract and refactor. You end up duplicating code instead of reusing it. You violate the DRY principle (Don’t Repeat Yourself). As a consequence, you keep growing your subtly different jungle of classes, and as you add inheritance levels, your classes get more and more arthritic and brittle. When you find a bug, you don’t fix it in one place. You fix it everywhere.

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

Define Closure.

A

In JavaScript, a closure is created whenever a function accesses a variable defined outside the immediate function scope.

It’s easy to create closures:

Simply define a function inside another function, and expose the inner function, either by returning it, or passing it into another function. The variables used by the inner function will be available to it, even after the outer function has finished running.

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

What is Prototypal OO?

A

The concept of building new instances NOT from classes, BUT from a compilation of smaller object prototypes.

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

Why is Functional Programming a natural fit for Javascript?

A
  • First-class functions
  • Closures
  • Simple Lambda Syntax
  • Assign Functions to variables
  • Compose Functions
  • Return Functions from other Functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are Pure Functions?

A

Functional Programming are built mostly with a handful of very small, very resuable, very predictable pure functions.

  • Idempotence - important feature for building RESTful web services, but it also serves the very useful prupose of separating computation from dependency on both time and order of operations - extremely valuable for parallel and distributed computation (think horizontal scaling).
  • Free from Side-effects - They do not mutate any shared state or mutable arugments, and other than their return value, they don’t produce any observable output, including thrown exceptoins, triggered events, I/0 devices, entwork, console, dispaly, logs, etc.

Pure functions produce stronger guarantees of encapsulation than objects without function pruity. Similar benefits as encapsulation in OOP - Ability to cahnge implemetnation without impacting the rest of the program.

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

Explain Reactive Programming

A

Reactive programming uses functioanl utilities like map, filter, and reduce to create and process data flows which propogate changes through the system - hence REACTIVE.

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

What is functional programming?

A

Functional programming produces programs by composing mathematical functions and avoids shared state and mutable data. Lisp was among first languages to support functional programming, and was heavily inspiried by lambda calculus.

Good to hear:

  • Pure functions/function purity
  • Avoid side-effects
  • Simple function composition
  • Examples of functional languages: Lisp, ML, Haskell, Erland, Clojure, Elm, F Sharp, OCaml, etc.
  • Mention of features that support FP: first-class functions, higher order functions, functions as arguments/values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define a Promise.

A

A promise is an object that provides a standard interface for dealing wtih values that may or may not be avaiable at the time the promise is used.

In other words, a promise wraps a potential value that may be resolved in the future.

fetchFutureStockPrices().then(becomeAMillionaire);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Difference between classical inheritance and prototypal inheritance?

A

Class Inheritance: Instances inherit from classes and create sub-class relationships: hierarchical class taxonomies. Instances are typically instantiated via constructor functions with the ‘new’ keyword.

Class Inheritance Flaws: Create tight coupling or hierarchies/taxonomies.

Prototypal Inheritances: Instances inherit directly from other objects. Instances are typically instantiated via factory functions or ‘Object.create()’. Instances may be composed from many different objects, allowing for easy selective inheritance.

Prototypes: mentions of concatenative inheritance, prototype delegation, functional inheritance, object composition.

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

Pros and Cons of OOP

A

OOP Pros: It’s easy to understand the basic concept of objects and easy to interpret the meaning of method calls. OOP tends to use an imperative style rather than a declarative style, which reads like a straight-forward set of instructions for the computer to follow.

OOP Cons: OOP Typically depends on shared state. Objects and behaviors are typically tacked together on the same entity, which may be accessed at random by any number of functions with non-deterministic order, which may lead to undesirable behavior such as race conditions.

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

Pros and Cons of FP

A

FP Pros: using the functional paradigm, programmers avoid any shared state or side-effects, which eliminates bugs caused by multiple functions competing for the same resources. With features such as the availability of point-free style (aka tacit programming), functions tend to be radically simplified and easily recomposed for more generally reusable code compared to OOP.

FP favors declarative and denottational styles, which do not spell out step-by-step instructions for operations, but instead concentrate on what to do, letting the underlying functions take care of the how. This leaves tremendous latitude for refactoring and performance optimization, even allowing you to replace entire algorithms with more efficient ones with very little code change. (e.g., memoize, or use lazy evaluation in place of eager evaluation.)

FP Cons: Over exploitation of FP features such as point-free style and large componsitions can potentially reduce readability becasue the resulting code is often more abstractly specified, more terse, and less concrete.

More people are familiar with OO and imperative programming than functinoal programming, so even common idioms in functional programming can be confusing to new team members.

FP has a much steeper learning curve than OOP becasue the broad propulatiry of OOP has allowed the language and learning materials of OOP to become more conversational, whereas the language of FP tends to be much more academic and formal.

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

When Classical Inheritance is approriate?

A

Never! “Favor object composition over class inheritance.”

17
Q

When is prototypal inheritance an approriate choice?

A

Each type of prototypal inheritance has its own set of use-cases, but all of them are equally useful in their ability to enable composition, which creates has-a or uses-a or can-do relationships as opposed to the is-a relationship created with class inheritance.

Types of Prototypal Inheritance:

  • Delegation (Prototype chain)
  • Concatenative (i.e. mixins, ‘Object.assign()’)
  • Functional (A function used to create a closure for private state/encapsulation)
18
Q

Javascript is a fusion of what three programming languages?

A
  1. Scheme (lambda)
  2. Self (prototypal inheritance)
  3. Java (syntax)
19
Q

What is a prototype, from Programming Javascript Applications book?

A

A prototype is an object intended to model other objects after. They can be used in 2 ways:

  1. You can delegate access to a single, shared prototype object (called a delegate)
  2. You can make clones of the prototype.
20
Q

Delegate Prototypes - from Programming Javascript Applications book?

A

In Javascript, objects have an internal reference to a delegate prototype. When an object is queried for a property or method, the Javascript engine first checks the object. If key doesnt exist on that object, it checks the delegate prototype, and so on up the prototype chain.

21
Q

What does “favor object composition over class inheritance” mean?

A

It means that code resuse should be achieved by assembling smaller units of functionality into new objects instead of inheriting from classes and creating object taxonomies.

In other words, use can-do, has-a, or uses-a relationships instead of is-a relationships.

Try to avoid class hierarchies, avoid brittle base class problem, avoid tight coupling, avoid rigid taxonomy, avoid gorilla banana problem.

22
Q

What are two-way data binding and one-way data flow, and how are they different?

A

Two way data binding means that UI fields are bound to model data dynamically such that when a UI field cahnges, the model data changes with it and vice-cersa.

One way data flow means that the model is the single source of truth. Changes in the UI trigger messages that signal user intent on the model (or “store” in React). Only the model has the access to change the app’s state. The effect is that the data always flows in a single direction, which makes it easier to understand.

Notes:

  • ReactJS is new/best example of one-way data flow.
  • Angular is popular framework which uses two-way binding.
23
Q

Difference between monolithic vs microservice architectures?

A

A monoloithic architecture means that your app is written as one cohesive unit of code whose components are designed to work together, sharing the same memory space and resources.

A microservice architecture means that your app is made up of lots of smaller, independent applications capable of running in their own memory space and scaling independently for each other across potentially many separate machines.

24
Q

What are the Monolithic Pros/Cons?

A

Pros:

  • Easy to hook up components to those cross cutting concerns such as loggined, rate limiting, and security features (such as audit trails and DOS protection)
  • Shared memory access is faster than inter-process communication.

Cons:

  • Tend to get tightly coupled and entangled as the application evolves, making it difficult to isolate services for purposes such as independent scaling or code maintainability
  • Harder to understand - May be depdendencies, side-effects, and magic whichi are not obvious when you’re looking at a particular service or controller.
25
Q

What are the Microservice Pros/Cons?

A

Pros:

  • Typically better organized, since each microservice has a very specific job, and is not concerned with the jobs of other components.
  • Decoupled services are also easier to recompose and reconfigure to serve the purposes of different apps (for example, serving both the web clients and public API).

Cons:

  • Lots of cross-cutting concerns that you did not anticipate at design time.
  • Prolifereation of VM wrangling work.
26
Q

What is asynchronous programming, and why is it important in Javascript?

A

Synchronous programming means that, barring conditions and function calls, code is executed sequentially from top-to-bottom, blocking on long-running tasks such as network requests and disk I/O.

Asynchronous programming means that the engine runs in an event loop. When a blocking operation is needed, the request is started, and the code keeps running w/o blocking for the result. When the response is read, an interrupt is fired, which causes an event handler to be run, where the control flow continues. In this way, a single program thread can handle many concurrent operation.

User interfaces are asynchronous by nature, and spend most of their time waiting for user input to interrupt the event loop and trigger event handlers.

Node is asynchronous by default, meaning that the server works in much the same way, waiting in a loop for a network request, and accepting more incoming requests while the first one is being handled.

27
Q

Does ‘new’ mean that code is using classical inheritance?

A

The ‘new’ keyward is used to invoke a contstructor but it also…

  • Creates a new instance
  • Binds ‘this’ to the new instance
  • Reference the new objects delegate [[Prototype]] to the object referenced by the contstructor function’s ‘prototype’ property.
  • Names the object type after the constructor, which you’ll notice mostly in the debugging console. You’ll see ‘[Object Foo]’, for example, instead of ‘[Object object]’.
  • Allows ‘instanceof’ to check whether or not an object’s prototype reference is the same object references by the .prototype property of the constructor.

Note: ‘instanceof’ lies - It does not do type checking the way you expect in strongly typed languages.