Skip to content

Instantly share code, notes, and snippets.

@jayjariwala
Last active January 17, 2018 02:02
Show Gist options
  • Save jayjariwala/f2555d900cd81150e215c2684481fa53 to your computer and use it in GitHub Desktop.
Save jayjariwala/f2555d900cd81150e215c2684481fa53 to your computer and use it in GitHub Desktop.
YDK JS - Scope and Closure
Program state: the ability of a language to store value in a variable and later retrive or modify those values. In fact,
the ability store values and pull values out of variables is what gives a program state.
where do those variables live?
where are they store?
how does our program find them?
The set of rules for storing varibales in some location, and for finding those variables at a later time is called scope.
In traditional compiled-language, a chunk of source code, your program , will undergo typically three steps before it is executed, roughly called "compilcation".
- Tokenizing/lexing (divide program into smaller sets e.g var, a , = , 2, ; etc..)
- parsing ( conver smaller sets into treelike structure also known as abstract sytanx three)
- code-genereation ( converting AST to executable code)
Compiler - Compilation
Engine - Execution
function foo(a) {
var b = a;
return a + b;
}
var c = foo( 2 );
LHS lookups -> c, a, b
RHS lookups -> foo(2.. , = a, a.. and b ..
ReferenceError - is scope resolution-failure related,
whereas - TypeError implies that scope resolution was successful, but that there was an illegal/impossible action attempted against the result.
LEXICAL SCOPE
There are two types of scops
1) Lexical scope - used by most of the programming languages
2) Dynamic scope - mostly used by scipting languages ( e.g perl, bash)
The first traditional phase of a standard language is called lexing (aka tokenizing).
Lexical scope is based onb where variables and blocks of scope are authored, by you, at write time, and thus is (mostly) set in stone by the time the lexer processes your code
CHAPTER 3
Function Versus Block Scope
CH 4
function declarations are hoisted but function expressions are not hoisted;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment