Você está na página 1de 2

code:

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.

>reference to the outer environemnt:


every execution context has a reference to its outer environment

**function b's outer environment is the global execution context. That is also the
case in function a

**the lexical environment - where something is written physically is important.


that it determines certain things as far as how the js engine decide and interpret
where things will live and sit in memory and HOW THEY WILL CONNECT TO EACH OTHER.

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.

Scope Chain - the chain of references to outer environments as determined by the


lexical environments.
Scope is dependent on the lexical environment of the declaration not of the call.
And perhaps, stacking of execution contexts is dependent on the order and not
n,ecessarily the lexical environment, of the calls.

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

who created me? that answers who is my outer environment

when a variable retutns an unexpected value, we can now debug it!!! :)

Você também pode gostar