الغلق في JavaScript
- Ɗanar ƙaƙara Ɗanar JS ƙanƙuwa
- Ɗanar ƙaƙara Ɗanar JS ƙanƙuwa
JavaScript ƙarima ceceLocal
OrGlobal
Scope.
Global variables can be accessed through闭包
Implement local (private).
Global variables
Functions can access functionsInternalDefined all variables, such as:
实例
function myFunction() { var a = 4; return a * a; }
But functions can also access functionsExternalDefined variables, such as:
实例
var a = 4; function myFunction() { return a * a; }
In the last example,a
IsGlobalVariables.
In a web page, global variables belong to the window object.
Global variables can be used and modified by all scripts on the page (and in the window).
In the first example,a
IsLocalVariables.
Local variables can only be used within the function they are defined in. They are invisible to other functions and script code.
Global variables with the same name as local variables are different variables. Modifying one will not change the other.
Not throughKeyword var
Variables created are always global, even if they are created in a function.
Variable lifecycle
Global variables live as long as your application (window, web page).
Local variables do not live long. They are created when a function is called and deleted when the function is completed.
A counter's dilemma
Suppose you want to use a variable to count, and you want this counter to be available to all functions.
You can use global variables and functions to increment the counter:
实例
// Initialize counter var counter = 0; // Increment counter function function add() { counter += 1; } // Call add() three times add(); add(); add(); // At this time, the counter should be 3
The above solution has a problem: any code on the page can change the counter without calling add().
For the add() function, the counter should be local to prevent other code from changing it:
实例
// Initialize counter var counter = 0; // Increment counter function function add() { var counter = 0; counter += 1; } // Call add() three times add(); add(); add(); // At this time, the counter should be 3. But it is 0.
It is not useful because we display the global counter instead of the local counter.
By making the function return it, we can remove the global counter and access the local counter:
实例
// Increment counter function function add() { var counter = 0; counter += 1; return counter; } // Call add() three times add(); add(); add(); // At this time, the counter should be 3. But it is 1.
It is not useful because we reset the local counter every time we call the function.
JavaScript internal functions can solve this problem.
JavaScript nested functions
All functions have the right to access the global scope.
In fact, in JavaScript, all functions have the right to access their 'upper' scope.
JavaScript supports nested functions. Nested functions can access their upper scope.
In this example, the internal function plus()
Can access the parent function's counter
Counter variable:
实例
function add() { var counter = 0; function plus() {counter += 1;} plus(); return counter; }
这样即可解决计数器困境,如果我们能够从外面访问 plus()
函数。
我们还需要找到只执行一次 counter = 0
的方法。
我们需要闭包(closure)。
الغلق في JavaScript
记得自调用函数吗?这种函数会做什么呢?
实例
var add = (function () { var counter = 0; return function () {return counter += 1;} })(); add(); add(); add(); // 计数器目前是 3
例子解释
变量 add
的赋值是自调用函数的返回值。
这个自调用函数只运行一次。它设置计数器为零(0),并返回函数表达式。
这样 add 成为了函数。最“精彩的”部分是它能够访问父作用域中的计数器。
这被称为 JavaScript 闭包。它使函数拥有“私有”变量成为可能。
计数器被这个匿名函数的作用域保护,并且只能使用 add 函数来修改。
ƙarƙa ba ƙaƙara ƙarƙa ƙaƙara, ƙaƙara ƙaƙara ƙaƙara ba a ƙaƙara ƙaƙara ƙaƙara ba, kuma ƙaƙara ƙaƙara ƙaƙara ba a ƙaƙara ƙaƙara ƙaƙara ba.
- Ɗanar ƙaƙara Ɗanar JS ƙanƙuwa
- Ɗanar ƙaƙara Ɗanar JS ƙanƙuwa