javascript reading Flashcards
Can you name two programming paradigms important to Javascript Application Developers?
- Prototypal Inheritance (also: protoypes, OLOO - Objects Linking to Other Objects)
- Functional Programming (also: closures, first class functions, lambdas)
Why are constructors messy in Javascript?
Constructors violate the open/closed principle because they couple all callers to the details of how your object gets instantiated.
What is a Factory Function?
Factory Functions are simply constructors minus the ‘new’ requirement, global pollution danger, and awkward limitations.
Does Javascript need constructor functions?
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.
Name some Classical Inheritance Issues.
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.
Define Closure.
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.
What is Prototypal OO?
The concept of building new instances NOT from classes, BUT from a compilation of smaller object prototypes.
Why is Functional Programming a natural fit for Javascript?
- First-class functions
- Closures
- Simple Lambda Syntax
- Assign Functions to variables
- Compose Functions
- Return Functions from other Functions
What are Pure Functions?
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.
Explain Reactive Programming
Reactive programming uses functioanl utilities like map, filter, and reduce to create and process data flows which propogate changes through the system - hence REACTIVE.
What is functional programming?
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
Define a Promise.
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);
Difference between classical inheritance and prototypal inheritance?
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.
Pros and Cons of OOP
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.
Pros and Cons of FP
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.
When Classical Inheritance is approriate?
Never! “Favor object composition over class inheritance.”
When is prototypal inheritance an approriate choice?
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)
Javascript is a fusion of what three programming languages?
- Scheme (lambda)
- Self (prototypal inheritance)
- Java (syntax)
What is a prototype, from Programming Javascript Applications book?
A prototype is an object intended to model other objects after. They can be used in 2 ways:
- You can delegate access to a single, shared prototype object (called a delegate)
- You can make clones of the prototype.
Delegate Prototypes - from Programming Javascript Applications book?
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.
What does “favor object composition over class inheritance” mean?
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.
What are two-way data binding and one-way data flow, and how are they different?
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.
Difference between monolithic vs microservice architectures?
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.
What are the Monolithic Pros/Cons?
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.