YDKJS scopes and closures Flashcards

(32 cards)

1
Q

why is it important to figure out is js compiled or interpreted?

A

because it will determine Scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are the compilation steps?

A

1 - tokenization/lexing: var a = 2 -> var, a ,= , 2
2 - creation of AST abstract syntax tree
3 - code generation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what are the 2 phases for js program?

A

1 - parsing/compiling

2 - execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how do we prove that js is compiled?

A

1 - syntax errors
2 - early errors
3 - hoisting

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how to modify scope at runtime?

A

1 - using eval

2 - with -> turns object into a block scope with entries as variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the scope of js called?

A

lexical scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is the scope of let and const

A

it’s nearest {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is the scope of var?

A

it’s nearest enclosing function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what happens when assigning a variable that was not declared before?

A

1 - non-strict mode will be created globally

2 - strict mode will throw a reference error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

when does a lookup stop for a variable?

A

once it finds the first declaration for it upwards

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what happens if we cannot find the declaration of a variable in any lexical scope during compilation?

A

the variable is left undefined until runtime because the variable might be declared in another file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is shadowing?

A

it’s when a block scope variable steps in for the outer scope variable which makes that outer scope impossible to modify

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how to access a global variable even if shadowed?

A

by using window.

but it only works for var and function nothing else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

can let shadow var?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

can var shadow let?

A

no unless it is outside the boundary of a function because var hops until in functions the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what happens to function expression with a name?

var askQuestion = function ofTheTeacher() ?

A

the function identifier is accessed only in the function scope and it’s value cannot be changed

17
Q

what does function expression with a named identifier called?

A

named function expression

var askQuestion = function ofTheTeacher()

18
Q

what does function expression with no identifier called?

A

anonymous function expression

var askQuestion = function()

19
Q

is there a difference in lexical scope between arrow functions and normal ones?

20
Q

what is hoisting?

A

the ability to access a variable in the beginning of the scope before it’s created

21
Q

what is the difference between function declaration hoisting and function expression hoisting?

A

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

22
Q

can you redeclare a var?

A

yes if only declared before as a var

23
Q

can you redeclare a let?

A

no even if the other declare is a var

24
Q

why can you redeclare a var and not a let?

A

just stylistic no technical reason

25
what is TDZ?
temporal dead zone period of time from the entering of a scope to where the auto-initialization of the variable occurs.
26
are let and const hoisted?
yes but not initialized but still cannot be accessed even with initialization call because of TDZ
27
is it necessary that every curly braces is a scope?
no it becomes a scope once a let or const is inside it
28
what is a behavior of closure?
closure is a behavior of function only and doesn't deal with class or objects
29
does a closure have to close over a function?
no it only has to close over an outer scope
30
are closures value oriented or variable oriented?
variable oriented they close over variables not value
31
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
32
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.