Execution Context EHSAN EHRARI What is an Execution Context?
• In JavaScript, an execution context is an abstract
concept that describes the environment in which JavaScript code is executed. • It consists of three components: 1. Variable Object (VO). 2. Scope Chain. 3. "this" keyword. Variable Object (VO)
• When JavaScript code is executed, an execution context
is created for it. • The execution context consists of a Variable Object (VO), which contains: 1. All Variables 2. Function Declarations 3. Function Arguments Scope Chain
• The Scope Chain is a list of Variable Objects that defines
the current scope of the execution context. • The Scope Chain is created by looking up the Variable Object of the current context and then following the chain of linked Variable Objects to parent contexts until the global context is reached. “this” Keyword
• The "this" keyword refers to the object that is currently
executing the JavaScript code. • It is determined dynamically based on how the function is called and can be different each time the function is invoked. Global Execution
• The global execution context is the default execution
context in a JavaScript program. • It is the first execution context that is created when the program starts running, and it remains active throughout the entire runtime of the program. Components of Global Execution Context
• The global object: In a web browser environment, the global object
is the window object. • The outer environment: The outer environment of the global execution context is null since there are no parent contexts. • The scope chain: The scope chain of the global execution context consists of the global object only. • The "this" keyword: In a web browser environment, the "this" keyword in the global execution context refers to the window object. Outer Environment
• In JavaScript, the outer environment refers to the
execution context that is one level higher in the scope chain. Example Example Example Example Example Example Example Conclusion
• Overall, the execution context is a fundamental concept
in JavaScript that helps to understand how variables and functions are scoped and accessed within a program. • Understanding execution contexts is important for writing efficient and well-structured JavaScript code. Question?