0% found this document useful (0 votes)
24K views

Lecture 16 - Conceptual Aside Javascript and Undefined

The document discusses JavaScript scope and lexical environments. It explains that when a function is executed, it creates a new execution context and variable environment. If a variable is not found in the current context, JavaScript will search the function's outer environment, which is determined lexically based on where the function is declared rather than where it is called. So even though function b() is called inside a(), its outer environment is the global context because it is declared at the global level rather than inside a().

Uploaded by

Tim Pron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24K views

Lecture 16 - Conceptual Aside Javascript and Undefined

The document discusses JavaScript scope and lexical environments. It explains that when a function is executed, it creates a new execution context and variable environment. If a variable is not found in the current context, JavaScript will search the function's outer environment, which is determined lexically based on where the function is declared rather than where it is called. So even though function b() is called inside a(), its outer environment is the global context because it is declared at the global level rather than inside a().

Uploaded by

Tim Pron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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!!! :)

You might also like