Private variables can use closures.
Functions can access variables defined inside the function, such as:
Functions can also access variables defined outside the function, such as:
In the latter example, a is a global variable.
Global variables belong to window objects in web pages.
Global variables apply to all scripts on the page.
In the first instance, a is a partial variable.
Local variables can only be used inside functions that define it. Not available for other functions or script code.
Global and local variables are two different variables, even if they have the same name. Modifying one will not affect the value of the other.If a variable is declared without the var keyword, it is a global variable, even if it is defined within a function.
Variable life cycle
Variables declared inside a function only work inside the function. These variables are local variables and the scope is local; the parameters of the function are also local and only work inside the function.
Imagine if you want to count some values and the counter is available in all functions.
You can use global variables, the function sets the counter to increment:
The counter value changes when the add () function is executed.
But here comes the problem. Any script on the page can change the counter, even without calling the add () function.
If I declare a counter inside a function, I cannot modify the value of the counter without calling the function:
The above code will not be output correctly. Every time I call the add () function, the counter will be set to 1.
All functions have access to global variables。
In this example, the embedded function plus () can access the counter variable of the parent function:
If we can access the plus () function externally, this will solve the dilemma of the counter.
We also need to make sure that counter = 0 is executed only once.
We need closures.
Remember the function calls itself? What does this function do?
variable add specifies the return word value of the function's self-invocation.
The self-calling function is executed only once. Set the counter to 0. And returns a function expression.
The add variable can be used as a function. The great part is that it has access to the scoped counters of the function.
The counter is protected by the scope of the anonymous function and can only be modified by the add method.
Closures are a mechanism for protecting private variables. When the function is executed, a private scope is formed to protect private variables inside from external interference. Intuitively, it is to form a stack environment that is not destroyed.