YDKJS scopes and closures Flashcards
why is it important to figure out is js compiled or interpreted?
because it will determine Scope
what are the compilation steps?
1 - tokenization/lexing: var a = 2 -> var, a ,= , 2
2 - creation of AST abstract syntax tree
3 - code generation
what are the 2 phases for js program?
1 - parsing/compiling
2 - execution
how do we prove that js is compiled?
1 - syntax errors
2 - early errors
3 - hoisting
how to modify scope at runtime?
1 - using eval
2 - with -> turns object into a block scope with entries as variables
what is the scope of js called?
lexical scope
what is the scope of let and const
it’s nearest {}
what is the scope of var?
it’s nearest enclosing function
what happens when assigning a variable that was not declared before?
1 - non-strict mode will be created globally
2 - strict mode will throw a reference error
when does a lookup stop for a variable?
once it finds the first declaration for it upwards
what happens if we cannot find the declaration of a variable in any lexical scope during compilation?
the variable is left undefined until runtime because the variable might be declared in another file
what is shadowing?
it’s when a block scope variable steps in for the outer scope variable which makes that outer scope impossible to modify
how to access a global variable even if shadowed?
by using window.
but it only works for var and function nothing else
can let shadow var?
yes
can var shadow let?
no unless it is outside the boundary of a function because var hops until in functions the function