Closures Flashcards

1
Q

What is a closure

A

Aclosureis the combination of a function bundled together (enclosed) with references to its surrounding state (thelexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

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

Types of closures?

A

Lexical Closures, Emulating Private Methods using closures

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

What is a lexical closure?

A

Lexical closure, is when we have an outer function and inside the outer function, we have a variable and another function and the inner function uses the local viarables in the outer funciton. It uses the location where a variable is declared within the source code to determine where that variable is available. Nested functions have access to variables declared in their outer scope.

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

What is the advantage of closures?

A
  • Closures are useful because they let you associate some data (the lexical environment) with a function that operates on that data. This has obvious parallels to object-oriented programming, where objects allow us to associate some data (the object’s properties) with one or more methods.
    Consequently, you can use a closure anywhere that you might normally use an object with only a single method.
  • To emulate private methods using closures. Private methods aren’t just useful for restricting access to code: they also provide a powerful way of man
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Module Pattern?

A

To use closures to define public functions that can access private functions and variables. Using closures in this way is known as themodule pattern. We must use closures within anonymous functions to make module patterns.

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

Provide an example of Module Pattern

A
Remember it has to be an anonymous function
var counter = (function() { // ->
  var privateCounter = 0;
  function changeBy(val) {
    privateCounter += val; //
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How many scopes do we have for each closure?

A

For every closure we have three scopes:
• Local Scope (Own scope)
• Outer Functions Scope
• Global Scope

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

Example of closure scope with anonymous functions

A
// global scope
var e = 10;
function sum(a){
  return function(b){
    return function(c){
      // outer functions scope
      return function(d){
        // local scope
        return a + b + c + d + e;
      }
    }
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Example of closure scope without anonymous functions

A
// global scope
var e = 10;
function sum(a){
  return function sum2(b){
    return function sum3(c){
      // outer functions scope
      return function sum4(d){
        // local scope
        return a + b + c + d + e;
      }
    }
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Disadvantages of closures?

A

It is unwise to unnecessarily create functions within other functions if closures are not needed for a particular task, as it will negatively affect script performance both in terms of processing speed and memory consumption.

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