Frontend and Javascript Trivia Flashcards
How does prototypal inheritance work in JS?
All JS objects have a prototype property
Objects Linked To Other Objects (OLOO): If an object doesn’t have the property you’re trying to access, JS engine will traverse the prototype chain until it finds the property
How do you write testable components?
Creating single purpose DRY components
Writing semantic HTML/CSS
- Using the appropriate tags to accurately reflect what the component contains
Object Oriented Programming
Description
- Examples: Prototypes, OLOOs (objects linked to other objects)
- Imperative (inheritance) programming: set of instructions for the computer operations
- Let’s say we already have a class that defines an Animal, and now we want to make a Duck. It is an animal, but it needs to be able to fly and quack.
- In inheritance world, Duck extends Animal and adds the ability to fly/quack. Now those behaviors are tightly coupled with each other.
Pros
- As code evolves, easily extend new functionality by creating new classes which extends existing classes
- Existing classes can be left alone
Cons
- Shared state may lead to side effects: different variables/things competing for the same resources
- Highly OOP codebase can be extremely resistant to change: and very brittle compared to an equivalent FP codebase.
Functional Programming
Description
- Examples: closures, first class functions, lambdas (anon functions)
- Declarative (compositional) programming: concentrate on what to do, letting underlying functions take care of how (i.e. makeSorted() is declarative)
- In composition world, you isolate behaviors, and can give a class superpowers by adding it in like so canQuack(canFly(Animal))
- This reduced the amount of coupling that occurs and makes it easier to reuse behaviors. You can make a Pilot with canFly(Person) (see image for React version).
Pros
- Avoids shared state, mutable data and side effects
- Encourage function purity: self contained and stateless
- Easier to test b/c can be tested in isolation with typical
Cons
- Can be harder to read
DNS (Domain name system)
The way to map URL string (www.google.com) to IP Addresses
Compile time vs. Run time
Compile time:
- Interprets higher level language and converts it to machine language (binary language) executable code
- Things like:
- syntax parser
- fixed expression evaluation
Run time:
- Executes compiled code
- Whatever the compiler cannot do (due to lack of information) will be done at run-time
- values depending on variables / user input
- accessing current time
Breadth First Search
Fail Fast Principle
You want to catch bugs earlier leading to fewer defects in production code because…
-
compile/build time errors still compile
- syntax errors
- data type errors
-
run time errors will crash the consumer
- logical errors
- cannot import file
What do you expect to happen here?
Classical Inheritance vs. Prototypal Inheritance
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance
Classical Inheritance:
- instances inherit from classes (Java / C++)
- create tight coupling or hierarchies/taxonomies
- class Student { constructor(name) }
-
cannot invoke instance methods on class definition:
- class Student { constructor(name) this.name=name;}
- let student = new Student(‘Sally’)
- right: student.name // Sally
- wrong: Student.name
Prototypal Inheritance:
- instances inherit directly from other objects
- Object.create() / Object.assign()
- Objects Linking to Other Objects (OLOO): All JS objects have a private prototype property which holds a link to another object (recursive).
- When trying to access a property of an object, the property will not only be sought on the object but on the prototype chain until either a property with a matching name is found or the end of the prototype chain is reached
Depth First Search
What is rendering?
Parse HTML & CSS (construct DOM tree):
- Parse HTML => DOM Tree
- Parse CSS => Styling rules
Render Tree (RT): Combine both into a data structure called Render Tree aka a Frame Tree
Layout of Render Tree: position the elements (i.e. when floating to the left, it’ll put the element on the left)
Paint the Render Tree: Actual operation of drawing the graphics and painting it onto the screen (visual output)
How does the DOM traverse its nodes?
Depth First Search
What is a ReSTful API?
- REpresentational State Transfer.
- Uses standard HTTP (Hyper Text Transfer Protocol) commands to perform commands (get, post, put, delete) or exchange data with a service over a network
- Supports multiple technologies for data transfer such as text, xml, json, image etc.
- No contract defined between server and client, so loosely coupled implementation.
- Principles
- Client-server: Separate UI concerns from data storage concerns to improve portability of UI across multiple platforms
- Stateless: Request form client to server must contain all information necessary to understand request. Cannot store any context on server and session state is kept on client
Rendering
Reduce render-blocking requests
- load 3rd party styles with link tag in HTML instead of with @import in JS
-
load “above the fold” critical CSS first
- load critical CSS in the head tag above HTML
- load non-critical CSS in the script tag below HTML
Server-side render non-interactive content
- Good for Blog posts: server sends HTML (additional JS will needed to be “painted”) and blog posts don’t usually have interaction logic (JS) so it can render right away especially if the response is already cached.
Requests
More CDNs: if you want to increase speed, then decrease the space between your servers and the user
Reuse server connections to speed up requests
Have the backend resize images before sending over the requested size to the frontend
- prevents unnecessarily transmitting large files
- prevents frontend from resizing images first before rendering
Have service worker check for cached request and skip the network
- if cache has request, respond with cachedResponse otherwise, make request
Javascript:
Shorter prototype chains: JS uses prototypal inheritance model and lookup time for properties that are high up on the prototype chain (or non-existent properties) will traverse the entire prototype chain
Reuse already loaded data: If you’re going from “all jobs page” to “edit jobs page”, persist the state between page transitions so you don’t have to fetch for data again
- if “jobs” are edited often, only show the read-only data while fetching the updated data
What do you expect?