JS Fundamentals 2 Flashcards
Async JS
Allows long-running tasks to run in the background without blocking other tasks.
JS Engine has a single thread - it is critical that it not be blocked.
Callback Function
A function passed into another function as an argument.
Ex: setTimeout(cb, timer)
Ex: Array.map(function(item){ })
Callback disadvantages
Callback hell - when cbs are chained together / nested
No implicit error state (unlike try/catch)
Arrow functions ‘this’
“Lexicially or Statically Bound”
Value is det. by WHERE it is DEFINED (enclosing context), not WHERE it is CALLED (regular fns).
Most useful for EVENTS and CALLBACKS.
Promises
Library to handle async code that makes it easier to determine when code succeeds or fails (resolve, reject). They have built-in mechanisms for chaining (.then) and error handling when called (.catch)
Async/Await
Syntax that builds on Promises and resembles synchronous JS
Destructuring Syntax
let numbers = [1, 2, 3];
let [one, two, three] = numbers;
- this creates one two and three as variables w/values 1, 2, and 3 respectively.
const apple = { type: ‘delish’, color: ‘red’ };
const { type, color } = apple;
- creates the variables type and color:
console.log(type) // ‘delish’
Template Literals
Backticks with ${ } used for string interpolation
Lexical Scope
Ability of a fn scope to access vars from its parent scope.
ES6 arrow fn syntax has lexical scope built in, regular functions have their own scope.
Unknown number of arguments
ES6 - ‘rest’ is indicated with …
This can only be used at the end of an argument list.
Spread Operator
ES6 - includes everything from the array, indicated with …
const array = […array1, …array2];
What type is the ES6 Class
The JS ‘class’ keyword creates a function not a ‘class’
ES6 Class members
Constructor
- called automatically when creating a ‘new’ instance, JS creates an empty one for you if you don’t create one
Static Methods
- can refer to the method w/o referring to an instance of the class, uses ‘static’ keyword
Prototype Methods
- must be referenced w/an instance
Getters and Setters
- accessor fns that work like obj literals
- use for validation - check something before setting the value
- use for more control - over how property is accessed and modified
ES6 Class Inheritance
2 types of classes - ‘base’ and ‘derived’
Derived classes use the ‘extends’ keyword and the ‘super’ keyword, allows you to reference the parent constructor and the method definitions from the base class.
ES6 Modules
ES6 introduced a native module system.
Import statements get hoisted.