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.