JavaScript Execution Context Flashcards
What is the scope chain in JavaScript?
The scope chain is the idea of the JavaScript engine traversing up the scopes of the execution contexts that a function is defined in, in order to resolve variables and functions invoked.
Is the scope chain multi directional?
No the scope chain only works one way.
From a function defined in global scope the execution context of the function can look up the scope chain to the global execution context to resolve variables and functions. But the global scope can not access what is inside the function unless it is returned.
What is a parser?
Parser: A Parser or Syntax Parser is a program that reads your code line-by-line. It understands how the code fits the syntax defined by the Programming Language and what it (the code) is expected to do. It checks if the syntax is correct.
What is a JavaScript engine?
JavaScript Engine: A JavaScript engine is simply a computer program that receives JavaScript source code and compiles it to the binary instructions (machine code) that a CPU can understand. JavaScript engines are typically developed by web browser vendors, and each major browser has one. Examples include the V8 engine for Google chrome, SpiderMonkey for Firefox, and Chakra for Internet Explorer.
What is the difference between a function declaration and a function expression?
Function Declarations: These are functions that are assigned a name.
Function expressions: These are anonymous functions usually assigned to a variable.
What are the two kinds of Execution Context in JavaScript?
Global Execution Context (GEC)
Function Execution Context (FEC)
What is the GEC and what does it do? Can there be more than one GEC for a JavaScript file?
Whenever the JavaScript engine receives a script file, it first creates a default Execution Context known as the Global Execution Context (GEC).
The GEC is the base/default Execution Context where all JavaScript code that is not inside of a function gets executed.
For every JavaScript file, there can only be one GEC.
What is FEC and what does it do? Can there be more than one in a JavaScript runtime?
Function Execution Context
Whenever a function is called, the JavaScript engine creates a different type of Execution Context known as a Function Execution Context (FEC) within the GEC to evaluate and execute the code within that function.
Since every function call gets its own FEC, there can be more than one FEC in the run-time of a script.
The creation of an Execution Context (GEC or FEC) happens in two phases, what are they?
Creation Phase
Execution Phase
The Execution Context creation phase occurs in 3 stages, what are they?
Creation of the Variable Object (VO)
Creation of the Scope Chain
Setting the value of the this keyword
What is a JavaScript execution context?
Execution context is a special environment created by the JavaScript engine that handles the transformation and execution of JavaScript code.
Does the browser natively understand high-level JavaScript code?
No, the browser doesn’t natively understand the high-level JavaScript code that we write in our applications. It needs to be converted into a format that the browser and our computers can understand – machine code.
While reading through HTML, if the browser encounters JavaScript code such as a script tag or an attribute that contains JavaScript code like onClick, what does the browser do?
It send the JavaScript code to the JavaScript engine to be run.
What does the execution context contain?
The Execution Context contains the code that’s currently running, and everything that aids in its execution.
What 4 things happen during the execution context runtime?
During the Execution Context run-time, the specific code gets parsed by a parser, the variables and functions are stored in memory, executable byte-code gets generated, and the code gets executed.