Object-Oriented Programming with JavaScript, Part II: Methods: Defining Contexts - Doc JavaScript
Object-Oriented Programming with JavaScript, Part II: Methods
Defining Contexts
JavaScript distinguishes between three execution contexts: Global code, Eval code, and Function code. Contexts are different from each other in the set of variables that are defined in each context. Each time the browser enters a new context, it creates a new object that holds the new context's variables and functions. This object is called the variable object. Any variables or functions defined within a context, are loaded onto this variable object. The variable object is deleted whenever the browser exits an executing context. Therefore, all variables and functions that are local to an executing context are destroyed upon exiting the context. There is no way to access the variable object. When you access variables and functions, you actually access the variable object's parameters and methods.
Global code is any code that is outside functions. Eval code is passed as a string to the eval()
function (eval(alert("bla bla bla")
). Function code is code within a function definition.
When the browser switches a context, a new scope is determined. The scope determines which variables, objects, and functions are accessible from the execution context. For example, suppose a variable a
is defined in both the main program and inside the function foo()
. If the variable a
is incremented inside the function foo()
, which a
is being incremented, the one defined inside foo()
, or the one defined in the main code?
The scope is determined solely by the the variable object that is being created upon entering the new execution context, and destroyed upon leaving it. The variable object holds variables and functions that are currently active. The scope also includes the this
object. When you refer to this
, which object are you referring to?
Next: How to classify scopes
Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: March 26, 2001
Revised: March 26, 2001
URL: https://www.webreference.com/js/column80/5.html