Home >Web Front-end >Front-end Q&A >What does jquery closure mean?

What does jquery closure mean?

WBOY
WBOYOriginal
2022-04-24 16:00:331276browse

In jquery, a closure refers to a function that has access to variables in the scope of another function and is a function that can read the internal variables of other functions; a closure can be understood as a function defined in a function , and can be accessed externally, which can extend the life cycle of variables and protect private variables. Because internal variables are inaccessible from the outside, modification of variables can be prevented.

What does jquery closure mean?

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

What does jquery closure mean

Closure definition - refers to a function that has access to a variable in the scope of another function.

A closure is a function that can read the internal variables (local variables) of other functions

jQuery closure principle

A closure is a function that has many An expression (usually a function) of variables and the environment to which these variables are bound, so that these variables are part of the expression.

Simply put, it is a function defined in a function and can be accessed externally

Advantages of closure

Can reduce the number of global variable objects , to prevent global variables from being huge in the past and making it difficult to maintain

Prevent modifiable variables, because internal variables are inaccessible from outside and cannot be modified, ensuring safety

Reading internal variables Variables, the other is to keep the values ​​of these variables in memory.

The closure in jquery is the external variable used by the function, which can be obtained without passing parameters.

How to write closures in jquery:

(function($){
$("div p").click();
})(jQuery);

The $ here is just a formal parameter, but jquery is a global variable, so it will be executed automatically without calling the function, or in two steps

It is converted into a normal function. Write the function first and then call it.

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of What does jquery closure mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn