Escolar Documentos
Profissional Documentos
Cultura Documentos
function b(){
console.log(myVar);
}
function a() {
var myVar = 2;
b();
}
var myVar = 1;
a();
output:
1
**remeber when we run b(), it creates a new execution context, puts it on the
stack, and puts into its variable environment any variables that are DECLARED.
output: the value at the global level, what was sitting on the global execution
context.
**when we request a variable, js does more than just look at the variable
environemnt of the currently executing context.
**function b's outer environment is the global execution context. That is also the
case in function a
fucntion b() lexically sits ontop of the global environment, it is not inside
function a. it is sitting right there at the same level as the line myVar = 1;
**js cares about the lexical environment when determinng the outer environment of a
code or function
*** the execution context don't regard where somehting is written physically! the
order of execution will determine how the execution contexts are created - the
stacking perhaps. when it comes to determining the outer reference of any execution
context, js cares about its lexical environment. perhaps has to do with the stack.
**when a variable is requested while running a code inside any execution context,
it will first look at its varible environment, then it will look at the outer
reference and look for variables there.
*** so the b() function sits lexically not inside a() but at the global level, so
its outer refernece is the global execution context. b() at the same time is
invoked inside a(),so in the execution stack, it is above a(). This are 2
different things.
code:
function a() {
function b(){
console.log(myVar);
}
var myVar = 2;
b();
}
var myVar = 1;
a();
output:
**immediately, it means that we can no longer call b(); say after a(); in the
global level
this will result to Uncaught referenceError: b is not defined