Karteikarten
closures

Karten in diesem Stapel (13)
- 1What is closureEvery function in JS forms a closure. A closure is the combination of a function with its lexical environment (variables in scope) when the function was declared. It closes over variables which are in scope at the point of time it’s declared.
- 2—Anonym func maintains a reference to its environment, to which myName belongs. That environment describes scope at the point of time it was declared, so describes all variables that it had access to when it was declared.
Anonym func was declared when createGreeter was executed , at this point of time myName was existed, because every func keeps a reference to its environment when it was declared we have access to myName variable even if createGreeter already finished. Hence, access to myName is possible, even after createGreeter() finished executing.
Whenever we create new function (anonym func in return) it closes over its environment, so, over variables it can reach at the point of time you declare it.
Closure saves variable names but not values, it’ll look up values only when closure func will execute, not when it’s declared
- 3What result, whyFor every timer we create new anonym func, in every func we lock in surrounding context. In that context we got global i variable, but only name, not value. Lock in only name and access to variable. Timer executes after 500ms and it will be after for loop is finished (finishing loop is faster). When 5 timers complete they will look up the value of i, so they will look value at this point of time, since loop is finished before any timer func executed, they all look up the same value of i and that is 5, because when loop is complete it is 5(not 4) because only if it is 5 we go out of loop, if it is 4 we set new timer instead.

- 4solution for for loop closure - old

- 5solution for for loop closure - modern

- 6What result, whyIFY let us create new function scope in which we can define variables. Inside IFY we create new environment, so every timer anonym func we create has new environment and because we now close over j we now memorize j and j is different for every self executing function because we self execute new function for every iteration, so for every iteration we create new anonym function ( line 2)and immediately execute it, we have new scope inside of that function for every iteration and hence new value is locked into j. So 5 anonym func in timer memorize different environments because we have different self exec func and different env always have j but value for j is different in every env.

- 7What result, whyWith let create block scope, new block scope is created for every iteration so anonym func saves it’s environment but it’s new env for every iteration, new block scope with new variable i. Every variable in every environment only has one value which never changes and in func we always look up the different values in the different environments.
Inside for loop new variable is created for every iteration and it just copies over the old variables, so variable from last iteration plus 1
- 8two common uses for closuresClosures are frequently used in JavaScript for object data privacy, in event handlers and callback functions, and in partial applications, currying, and other functional programming patterns.
- 9closures in functional programmingIn functional programming, closures are frequently used for partial application & currying.
- 10Partial application and closure scopeApplication: The process of applying a function to its arguments in order to produce a return value.
Partial Application: The process of applying a function to some of its arguments. The partially applied function gets returned for later use. Partial application fixes (partially applies the function to) one or more arguments inside the returned function, and the returned function takes the remaining parameters as arguments in order to complete the function application.
Partial application takes advantage of closure scope in order to fix parameters. You can write a generic function that will partially apply arguments to the target function. It will have the following signature:
partialApply(targetFunction: Function, ...fixedArgs: Any[]) =>
functionWithFewerParams(...remainingArgs: Any[]) - 11when closure is createda closure is created whenever a function accesses a variable defined outside the immediate function scope.
- 12how to create closureSimply define a function inside another function, and expose the inner function, either by returning it, or passing it into another function. The variables used by the inner function will be available to it, even after the outer function has finished running.
- 13closures and data privacyClosures are the primary mechanism used to enable data privacy.
When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can’t get at the data from an outside scope except through the object’s privileged methods. In JavaScript, any exposed method defined within the closure scope is privileged.
You can use closures to create data privacy in JavaScript using a factory function.
Informationen
- Beschreibung
- Closures Karteikarten am closures, erstellt von milly larcen am 13/04/2020.
- Erstellt von
- milly larcen
- Aufrufe
- 0
- Bewertung