Closures Flashcards

1
Q

What is a closure?

A

It’s a feature that allows a function to remember the variables and functions that were present in its parent scope, even after the parent function has returned.

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

What is the purpose of closures?

A

They provide a mechanism to access variables from an outer function in an inner function, enabling data privacy and the ability to maintain state. They are a powerful tool for creating modular and reusable code.

Data privacy:
By creating functions that have access to variables from their parent scope, closures allow you to create private variables and functions that are not directly accessible from outside.

Private variables:
Achieve this by implementing data hiding, and maintaining state in functional programming. They enable the concept of “lexical scoping,” where functions remember the variables in their scope when they were defined.

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

What is the difference between a closure and a regular function?

A

A closure has access to variables in its outer lexical scope
A regular function does not.

Closures are created when a function is declared inside another function, whereas regular functions are standalone functions.

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