Execution Context and Scope
Execution Context and Scope
Execution Context and Scope
Execution context refers to the data that a variable or function has access to.
Each context has a variable object, upon which all of its defined variables and
functions exist
This variable object cannot be accessed via code as this takes place behind the
scenes
The global context is the outermost context in web browsers, the global context
is the window object.
Each function call has its own execution context. When code execution flows into
a function, that functions context is pushed onto a context stack. After the
function is finished executing, the context stack is popped and the returning
control goes to the previous context.
A scope chain refers to such a hierarchy of different contexts, with the front of
the chain being the current active context, with the global context always being
last in the chain
Identifiers are resolved by traveling up this scope chain, starting with the current
context, searching for the identifier name until it is found (if the identifier is not
there, this usually results in an error). The opposite is not true; an outer context
CANNOT access identifiers in tiers below (e.g. context from window cannot
access any identifiers in functions, since they would be nested within this context
by definition) An inner context can access everything
Though there are two primary types of execution contexts, global and function
(and the third exists inside a call to eval()), we can augment the scope chain,
temporarily adding a context to the front of the scope chain the code within that
context is done executing. This is done by
o The catch block in a try-catch statement
o A with statement
Both of these statements add a variable object to the front of the scope chain.
For the with statement, the specified object is added to the scope chain. For the
catch statement, a new variable object is created and contains a declaration for
the thrown error object
JavaScript does not have block-level scoping meaning declarations inside of if,
while, for-loop statements do not affect the scope chain
for (var i=0; I < 10; i++) {
doSomething();
}
alert(i); //10 this is valid!
********************************************************************************************
5 Reference Types
Reference values, also known as objects, is an instance of a specific reference
type. Since ECMAScript lacks constructs such as classes and interfaces, it is
incorrect to call reference values classes, though they are very similar.
Reference types are also sometimes called object definitions because they
describe the properties and methods that an object should have
Objects are considered to be instances of reference types
New objects are created using the new operator followed by a constructor.
A constructor is a function whose purpose is simply to create a new object.
var person = new Object();
The code above creates a new instance of the Object reference type and stores
it in the person variable
var person = {}; can be used to rewrite new Object(), though this is
uncommon
Developers tend to favorite object literal notation since it is concise and
encapsulates all of the data. It is also a way to pass a large number of optional
arguments to a function
function displayInfo(args) {
var output = ;
alert(output);
}
displayInfo({
name: Nicholas,
age: 29
});
displayInfo({
name: Greg
});
The function above accepts arguments that have a name property, age property,
both, or neither. This pattern of argument passing is best used whe there are a
large number of optional arguments. In other cases, named parameters work
fine, but can get unruly when a large number are present
Like many other OOP languages, dot notation is used to access object
properties, as well as bracket notation with strings. Both are identical. The
advantage to bracket notation is that it allows for named variables to access
properties, as well as reserved keywords and other illegal characters. Dot-
notation is preferred, unless something can only be accessed via bracket.
alert(person.name); //David
alert(person[name]); //David
That last line also returns the comma separated string listing. Alert expects a
string so the arrays toString() method is called directly