Rumah >hujung hadapan web >tutorial js >Bagaimana untuk Menyelesaikan Isu dengan Rujukan Pembolehubah Penutupan dalam Gelung JavaScript?
JavaScript Closure Inside Loops: Understanding the Problem and Its Solutions
The issue arises when using closures inside loops with variable declarations using the var keyword. Closures capture the environment they are defined in, including variables, and create a reference to them, even after the function has exited. This can lead to unexpected behavior when the captured variable changes value during the loop's execution.
The Problem:
Consider the following code:
<code>for (var i = 0; i < 3; i++) { funcs[i] = function() { console.log("My value:", i); }; }</code>
This code creates a loop that iterates three times. Inside each iteration, a function is defined and stored in an array. The expected output would be:
<code>My value: 0 My value: 1 My value: 2</code>
However, the actual output is:
<code>My value: 3 My value: 3 My value: 3</code>
This is because the variable i is captured by the closures, and it gets updated to the final value of 3 before any of the closures are executed.
ES6 Solution: Using 'let' Keyword
ECMAScript 6 (ES6) introduces the let keyword, which creates block-scoped variables. In our example, we can use let instead of var to create a new variable i for each iteration:
<code>for (let i = 0; i < 3; i++) { funcs[i] = function() { console.log("My value:", i); }; }</code>
This time, each closure will capture its own distinct i variable, and the expected output will be obtained.
ES5.1 Solution: Using 'forEach'
JavaScript's Array.prototype.forEach method provides a clean way to iterate over an array. Each callback function passed to forEach gets a distinct closure around the current element:
<code>var someArray = [ /* whatever */ ]; someArray.forEach(function(element) { // Code specific to this element });</code>
Classic Solution: Using Closures
Closures can be used to bind the variable to a specific value outside the function:
<code>function createfunc(i) { return function() { console.log("My value:", i); }; } var funcs = []; for (var i = 0; i < 3; i++) { funcs[i] = createfunc(i); }</code>
Here, an i-specific closure is created using createfunc and then stored in the funcs array. When each of these closures is executed, they refer to the corresponding i variable, resulting in the correct output.
Atas ialah kandungan terperinci Bagaimana untuk Menyelesaikan Isu dengan Rujukan Pembolehubah Penutupan dalam Gelung JavaScript?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!