Section 2: Execution Contexts and Lexical Environments Flashcards
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.
Object
A collection of name value pairs. the simplest definition when talking about Javascript.
This is in Javascript, Object might be different or more complicated in other programming languages.
When a javascript file is run, an execution context is created. What two things are created for you when that happens?
- A Global object
2. a special variable called ‘this’
Global Object
Base execution context. There will be a different global object if you’re running on a server but there will always be a global object when running Javascript.
Global Object is available to ALL code running inside the javascript file, the lexical environment.
Global object inside browsers is the ‘window’ object and ‘this’ then refers to the global object, ‘window’.
Global object means “not inside a function”
What happens to variables and functions when lexically they’re not sitting inside a function?
They become attached to the window (when running javascript in a browser) or global object.
So, ‘this’ is the window object.
Hoisting
tbd…
The execution context: creation phase
Phase 1 of execution context
Global object setup, ‘this’, Outer Environment
Also sets up memory space for variables and functions aka “hoisting”
Javascript and ‘undefined’
‘undefined’ is a special value in javascript that means the value of the variable has not been set.
DIFFERENT than a reference error… if a variable is set up with the memory space during execution context creation phase, the variable will have a value called ‘undefined’
When should you declare a value of ‘undefined’?
NEVER
the execution context: execution phase
Phase 2 of execution context
Runs your code line by line, interpreting it, converting it, compiling it, executing it on the computer into something the computer can understand.
single threaded (execution)
One command is being executed at a time
synchronous execution
One [line of code] at a time AND in order
[function] invocation
running or “calling” a function
In Javascript, by using parenthesis ()
Every invocation creates a new execution context which goes through both phases and gets put on the (top of the) execution stack.
Variable environment
Where the variables live.
And how they relate to each other in memory.
Scope
Where a variable is available in your code
and if it’s truly the same variable, or a new copy