Foundations Flashcards
Scope
- Global scope
- Local Scope/Function Scope
- Block scope
Closures
- How to create closures? a combination of the lexical environment
- When is it created? creation phase
- Why do we need closures? data privacy, event handlers, and callback functions.
Hoisting
- Moving declaration to the top(incorrect)
- Allow you to use a variable before declaring it
- function expressions are not hoisted
- Three Types: var(assign undefined), let/const(no initialized), function
Event Loop
- A Design pattern
- Monitor call stack and the task queue
- Push event to call stack when it’s empty
Task Queue
- A new JavaScript program or subprogram is executed (such as from a console, or by running the code in a element) directly.
- An event fires, adding the event’s callback function to the task queue.
- A timeout or interval created with setTimeout() or setInterval() is reached, causing the corresponding callback to be added to the task queue.
Microtasks Queue
- JavaScript promises
- Mutation Observer API
- Can use queueMicrotask() by other third parties libs
Macrotasks Queue
Does not exist
Lexical Environment
A lexical environment is basically the scope or environment the engine is currently
reading code in. A new lexical environment is created when curly brackets {} are used,
even nested brackets {{…}} create a new lexical environment. The execution context tells
the engine which lexical environment it is currently working in and the lexical scope
determines the available variables.
Execution Context
- A simple environment that the code run-in
- Code is always in an execution context
- Two/Three Types, Global/Function and eval
- Two Stages (creation phase and execution phase)
Types
- Undefined
- Null
- Boolean
- Number
- BigInt
- String
- Symbol
- Function Object
- Object
Scope Chain
Each execution context that has a link outside of it lexical environment called scope chain.
Dynamic Scope
this keyword
Primitive values
1. Type string number bigint boolean null undefined symbol 2. passed by value 3. copied and passed somewhere else in the memory 4.
Non Primitive
- passed by reference (pointer)
- To make is primitive we can
Object.assign
{…} - Only Shallow Copy, not Deep Copy
Currying
Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c)