Execution Contexts and Lexical Environments Flashcards
What is a syntax parser?
A program that reads your code and determines what it does and if it’s grammar is valid. Your code isn’t magic someone else wrote a program to translate it for the computer.
What is the lexical environment?
Where something sits physically in the code your write. ‘Lexical’ means ‘heaving to do with words or grammar’. A lexical environment exists in programming languages in which where you write something is important.
What is the execution context?
A wrapper to help manage the code that is running. There are lots of lexical environments. Which one is currently running is managed via execution contexts. It can contain things beyond what you’ve written in your code.
What is a name/value pair?
A name which maps to a unique value. The name may be defined more than once, but only can have one value in any given context. That value may be more name/value pairs.
What is an object?
A collection of name value pairs. This is the simplest definition when talking about javascript.
What is the base execution context?
The global execution context.
What is the global execution context?
The context that is available everywhere to everything in your code. This basically means “not inside a function” - code or variables that aren’t inside a function.
What does the global execution context create for you?
The javascript engine creates the global object.
When running javascript in the browser, what does the global object refer to?
The ‘window’ object.
What is ‘this’ equivalent to in the global context?
Window
What do you expect this code to output and why?
The result is due to hoisting. Even though function b is executed before it’s declared lexically, which would cause an error in most languages, in javascript function B is hoisted to the top in its entirety and variable a is hoisted as well, but not it’s value, which is why a only returns undefined.
What is hoisting?
Hoisting is a confusing term because it’s not actually moving code to the top of the page. It is referring to the result of the creation phase before your code is executed line-by-line in which the javascript engine has already set aside memory space for the variables and functions you’ve written. It does this so that once it starts executing line-by-line it can access them.
What is the main difference between variables and functions during hoisting?
Functions in their entirety( the function, its name, and the code inside the function to be executed) is placed into memory.
Variables, however, only have their name set aside in memory and their value is set to undefined. The value isn’t set until the execution phase.
What do you expect the output of this code to be and why?
Console.log(a) produces a reference error because it has not even been defined in memory space yet. If the variable a had been declared or even defined, it would have simply resulted in undefined because during hoisting memory space for its name would’ve been set aside, but the value wouldn’t be declared until the execution phase.
What are the two stages of an execution context?
- Creation Stage - When function is called, but before it executes any code inside.
- Create the scope chain
- Create variables, functions and arguments
- Determine the value of “this”
- Activation/Code Execution Phase:
- Assign values, references to function
- Interpret/execute code