Closures 11.9 Flashcards

1
Q

What is the difference between a bound variable and a free variable?

A

bound variables are defined within the scope, aka paramaters and local variables. free variables are defined outside of the scope., aka non-local variables

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

What is a closure

A

A closure is a function plus a connection to the variables that exist at the functions birth place or it’s lexical environment.

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

Explain what is happening here:

function funcFactory(value) {
return () => {
return value
}
}

const func = funcFactory(‘abc’)
assert.equal(func(), ‘abc’)

A

the return of funcFactory (a closure) is being assigned to variable func. because func is still connected to variables at it’s birthplace, it can still access the free ‘value’ variable )even though func escaped it’s scope).

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

What is a use case for closures?

A

you can use a closure to store states and make updates to them via function calls.

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