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
what happens to function expression with a name?
var askQuestion = function ofTheTeacher() ?
the function identifier is accessed only in the function scope and it’s value cannot be changed
what does function expression with a named identifier called?
named function expression
var askQuestion = function ofTheTeacher()
what does function expression with no identifier called?
anonymous function expression
var askQuestion = function()
is there a difference in lexical scope between arrow functions and normal ones?
no
what is hoisting?
the ability to access a variable in the beginning of the scope before it’s created
what is the difference between function declaration hoisting and function expression hoisting?
1 - function declaration identifier n is hoisted and initialized to its function value (again,
called function hoisting).
2 - A var variable is also hoisted, and
then auto-initialized to undefined
can you redeclare a var?
yes if only declared before as a var
can you redeclare a let?
no even if the other declare is a var
why can you redeclare a var and not a let?
just stylistic no technical reason
what is TDZ?
temporal dead zone
period of time from the entering of a scope to where the auto-initialization of the variable occurs.
are let and const hoisted?
yes but not initialized but still cannot be accessed even with initialization call because of TDZ
is it necessary that every curly braces is a scope?
no it becomes a scope once a let or const is inside it
what is a behavior of closure?
closure is a behavior of function only and doesn’t deal with class or objects
does a closure have to close over a function?
no it only has to close over an outer scope
are closures value oriented or variable oriented?
variable oriented they close over variables not value
how does a closure get treated in a loop?
1 - if defined by var there is only one variable for the whole loop so it will be invoked by the last value
2 - if defined by let there will be a new variable for each iteration
what is a closure?
Closure is observed when a function uses variable(s) from outer scope(s) even while running
in a scope where those variable(s) wouldn’t be
accessible.