문제

This is an academic question concerning javascript.

The code segment below takes an array of integers, and returns an array of pointers to functions. The functions pointed to are suppose to return the squared value of the array.

If the input was listOfFunction = sqFnList([1,2,3,4,5])

Then listOfFunction[0]() should return 1

and listOfFunction[1]() should return 4

but it does not.

function sqFnList(a){
   var b = [];
   for ( var i = 0; i <a.length; i++)
   {
      var sq = a[i] * a[i];
      b[i] = function() { return sq;}
   }
   return b;
}

 x = sqFnList([3,4,5])[0]()

The issues is that x = 25. Thanks for the help in advance.

도움이 되었습니까?

해결책

Your functions all contain a reference to the same variable sq, which has the value 25 once you actually call the functions. You need to create a separate closure for each function, each with its own copy of sq's value, in order to capture the value of sq at the time you want:

function sqFnList(a){
   var b = [];
   for ( var i = 0; i <a.length; i++)
   {
      var sq = a[i] * a[i];
       b[i] = function (the_sq) {
          return function() { return the_sq;}
       }(sq);
   }
   return b;
}

x = sqFnList([3,4,5])[0]()  // x is 9

Here's a fiddle to demonstrate.

다른 팁

sq is scoped to sqFnList and you keep updating the value, changing it in all the closures. You need to force a new scope for sq:

function sqFnList(a){
   var b = [];
   for ( var i = 0; i <a.length; i++)
   {
      var sq = a[i] * a[i];
      (function(sq) {
        b[i] = function() { return sq;}
      })(sq);
   }
   return b;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top