Javascript Hard Parts Flashcards
What are 3 main parts of Javascript engine?
- Thread of Execution
- Memory/Variable Environment
- Call Stack
- What does it mean that Javascript is Lexical or Static Scoping
- All the data from that scope gets backpacked
* We bring the data with us from when the function was born to wherever it goes hidden in the backpack
- What does NEW Keyword do?
- Creates empty object and assign to THIS
- Has hidden property _proto _ “ dunderscore proto”
- Create a function + Object combo
- Automatically Returns the object to global memory
- Arrow Function
- THIS is the same as where it was defined
- Arrow function always get whatever the THIS Keyword context, that what it gets
- Arrow function has implied return
- What is This?
- This refers to the object to the left of the dot in that execution context. Ex: user1.increment()
- If no dot, this defaults to the window object
- Biggest Mistake is calling a function in a method and THIS is referring to Window
- What is Class?
- It is Syntactical sugar.
- There is a prototype chain that creates a link that allows the ability to look up the chain for code
- This is similar to Object Oriented Programming and the concept of inheritance where knowledge is passed down but this is just a link to look up stuff
- The Cycle of Code Queues
- Thread of Execution runs through each lines of code
- The Event Loop checks Call Stack if it has anything to run then the global code
- Then it checks the microtask queue ( Promise-deferred functions) , then Callback Queue
- Event Loop prioritizes functions in the microtask queue over the callback queue
- What is Promises ?
• Promises are invoked when there is initiated background web browser work/functionality and a place to store data in Javscript memory
• There is a placeholder object on JavaScript
• Fetch creates a promise object ( that object contains value and hidden property) and also sets up network request in web browser feature
• OnFulfillment is the hidden property of Promises ( also onRejection)
• Promise objects will automatically trigger the attached function to run (w/ the input being the returned data)
• We will call THEN method upon completion. .THEN gets a function into the unfulfilled array (Then can be considered “Run Later” )
o Any code we want to run on the returned data must also be saved on the promise object
• Promises you that it would let you know when such browser functionalities end, exits and could also provide you a way to return values
- What is Asynchronous Code?
- Asynchronous Code means doing code out of order from when you saw it
- There is web browser feature request and at the same time in JavaScript, it returns a Promise Object which acts as a placeholder object.
- What is Thread of Execution?
- Goes through the code line by line and runs/executes each line
- When it reaches a function, it does not read the code unless the function is called and continues to next line
- JavaScript is synchronous by default and is single threaded. This means that code cannot create new threads and run in parallel.
- What is Closure?
- Functions get a new memory every run/invocation
- When a function gets called, we create a new live store of data (local memory/state) for that function’s execution context
- When the function finishes executing, its local memory gets deleted except for the returned value
- Local memory gets backpacked along with function
- If our function could hold live data between executions, this would let our function have an associated cache/persistent memory
- All the relevant data from local memory execution context is stored in that function.
- Closure allows a function to have attached a permanent memory store data
- Persistent Lexical Static Scope Reference Data AKA Backpack AKA Function’s Closure