What a closure is, what are its features, and how does it affect the page

Updated on technology 2024-02-25
4 answers
  1. Anonymous users2024-02-06

    A closure is when another function is defined inside a function, and the inner function is returned as a function object, while also storing the state information of the outer function. This inner function can depend on the variables and parameters of the outer function, and the outer function returns a reference to the inner function. This way of defining a function inside a function and returning it is called a closure.

    The definition format of the closure

    Closures in Python can be defined in the following format:

    def outer_function(x):

    def inner_function(y):

    return x + y

    return inner_function

    In this example, the outer function is the outer function, and the inner function is the inner function, which returns a reference to the inner function. When we pass an argument x to an outer function, it returns an inner function. Because the inner function retains the state of the variable x in the outer function, we can continue to use this variable when calling the inner function.

    Advantages of closures

    1. The state information of the outer function can be preserved: Since the inner function retains the state information of the outer function, the closure can be used to create some objects that remain in a posture deformed state in multiple calls, such as decorators.

    2. Can make the parameters of the function more flexible: The parameters of some functions may be fixed, but sometimes the value of the parameters needs to be changed during the function call. Closures can make the parameters of a function more flexible by saving the parameters and variables of the outer function traces.

    3. The structure of a large program can be simplified: by using closures, a large program can be split into multiple small functions, and variables and parameters can be shared between them.

  2. Anonymous users2024-02-05

    The three types of closures in discrete mathematics are found as follows:

    The matrix operation rule of symmetric closures: the relation r is symmetrical if and only if r's relation matrix (rij)n n is a symmetric matrix, i.e., r[i][j]=r[j][i].

    The matrix operation rule for transitive closures: the relation r is transitive if and only if in the relational matrix of r, for any i,j,k, if rij = 1 and rjk = 1, there must be rik = 1

    The matrix operation rule for reflexive closures: the relation r is reflexive if and only if the main diagonal of the relation matrix of r is all 1

    Closure arithmetic, what is a closure? Citation: Let r be a binary relation on a, and we want r to have certain useful properties, such as reflexivity.

    If R is not reflexive, R can be modified by adding a subset of ordered pairs to R to get a new relational R', such that r'It is reflexive. But I don't want R'It's too different from r. Okay.

    In other words, add as few ordered pairs as possible to meet these requirements'A reflexive closure called r, a closure constructed by adding an ordered pair has symmetrical closures and transitive closures in addition to reflexive non-closures. Let R be a binary relation on A, and the reflexive (symmetric, transitive) closure of R is the relation R', so that r'is reflexive (symmetrical, transitive); r'Contains r; to any reflexive (to, to) relation r'', if r''Contains r, then r''Contains r; '。

  3. Anonymous users2024-02-04

    This article summarizes the essential characteristics of closures through several cases.

    In this case, exp is used to receive a reference to the obj object, and we don't know if the obj object is cleaned up after the function call is over.

    It is proved that even if the function reference is ended, there is still a reference (assignment) to the internal reference type (array, object, function raider, etc.) outside the function, and the object (obj) involved in the object (addage) will not be eliminated for the time being.

    In addition to using global variables to scramble the reference type inside the function, we can also get the reference type we want outside the function --return

    Here exp is the object returned by the function, since one of the main purposes of using closures is thisManipulate private variables, it must be.

    With functions, if you don't need to return properties in an object and private properties together, you can do it directly.

    Returns a function.

    The name of the return function is generally useless, and if there are no special requirements, it is recommended to use the anonymous function, as follows.

    This is the most common form of closure.

    Having said all this, my understanding of the nature of closures can only be said in one sentence:If the variable x outside the function refers to the reference type y in the function, and the reference type y operates on the private variable, then the private variable will not ** with the function call, but will remain until x is no longer referencing y (see my article.)"Understanding about closures"), for example, assign {}, null

  4. Anonymous users2024-02-03

    Understanding of closures: A closure is a function that can read the internal variables of other functions.

    Closures are a mechanism to protect private variables, and when the function is executed, a private scope is formed to protect the private variable from external influences. Internal functions can use arguments and variables of external functions, and functions and variables are not spam by the mechanism.

    The value of a closure is that it can be used as a function object or as an anonymous function, which in the case of a type system means representing not only data but also **. Most languages that support closures treat functions as first-level objects, meaning they can be stored in variables, passed as arguments to other functions, and most importantly, can be dynamically created and returned by functions.

    The function is: anonymous self-executing function: we know that all variables, if you don't add the var keyword, will be added to the properties of the global object by default, and there are many disadvantages to adding such temporary variables to the global object, such as:

    These variables can be misused by other functions; This makes the global object too large and affects the access speed (because the value of the variable needs to be traversed from the prototype chain).

    Effect of closures on the page:

    1. Prevent global pollution.

    2. Internal functions can use external variables or parameters.

    3. Internal variables will not be garbage **mechanism**.

    4. Closures can update the values of external variables.

    5. The private method can be simulated with the closed hand forest bag.

    6. Because the closure will make the variables in the function be stored in memory, and the memory consumption is very large, the closure cannot be abused, otherwise it will cause the problem of page performance and performance; Closures, like global variables, can cause memory leaks if used in large quantities.

Related questions
14 answers2024-02-25

The postage of SF Express is not required to pay for postage, just the price of the product. >>>More

4 answers2024-02-25

Spending a lot of time on the computer every day can cause rapid loss of vision. Children should not exceed 2 hours a day, and adults should not exceed 3 and a half hours, which is very bad for vision.

4 answers2024-02-25

Hello, it's about"Dreams"The views and recommendations with "Dream Interpretation" are as follows: >>>More

9 answers2024-02-25

What is the answer to the guts of football:

1. "Steady courage" refers to the game where the difference in strength between the two sides is obvious, and after eliminating the possibility of an upset, the dominant side is selected. >>>More

7 answers2024-02-25

Photography is the use of machinery instead of eyes to see (and preserve) things.