JavaScript javascript function closure working javascript function closure explained,javascript function closure,javascript function closure

I was going through a javascript book and have encountered the following code on closure

function constFunc() {
    var funcs = [];
    for (var i = 0; i < 10; i++) {
        funcs[i] = function () {
            return i;
        }
    }
    return funcs;
}
var f = constFunc();
f[5]();

The output was not 5. The explanation given was "nested functions do not make private copies of the scope". i would be grateful if could someone explain the above statement.

Answer:1

When you call constFunc the loop runs and assigns a function to each element of the funcs array. Those functions each return the value of i, which changes on each iteration of the loop from 0 up to 9.

That means that once constFunc has finished executing i will have the value of 9. When you call one of the functions in the array it returns the value of i which as we've just seen is now 9. The value of i is not captured on each iteration of the loop.

You can force it to be captured via a closure in the loop:

for (var i = 0; i < 10; i++) {
    (function (i) {
        funcs[i] = function () {
            return i;
        };
    }(i));
}

In this case you are effectively creating a new variable with the same value as i on every iteration of the loop. You can see that in action here.

By passing i into a function you end up copying the value of it. So on the first iteration i has the value 0. We pass that value into the new function (the argument i is a different variable to the i declared in the loop initialiser) and then return that value from the array function. This way, each array function is returning a different i, instead of each returning the same i.

Answer:2

WHy did you expect the output of 5? RIght, because at the time the function was created, i was 5. Now, as we can see, the output is not 5 (but, if I didn't miss anything, 9). Now why is that?

The iinside the closure is not evaluated when the function is created, but when it runs. Now this is where the statement you qoted comes into play: there is only ONE i- the one created inside constFunc. All closures work on that one. After constFunc si done working, that i has, obviously, the value of 9 (becaus that's where the loop stops). SO all closures now output 9.

A simpler example:

function outer () {
    var i = 0;
    function inner () {
        return i;
    }
    i = 99;
}

outer();
inner(); // <- return 99, not 0.
Answer:3

Hi I'm building some custom function for my Easy PHP Calendar Most of the source code is encoded so i use the database directly to Get the needed events rows. My problem is translating the date ...

Hi I'm building some custom function for my Easy PHP Calendar Most of the source code is encoded so i use the database directly to Get the needed events rows. My problem is translating the date ...

  1. unknown date format yyyy-mm-dd joi
  2. unknown date format bad header data
  3. unknown date format unix
  4. unknown date format
  5. unknown date format iso 8601-strict
  6. unknown date format git
  7. unknown date format iso-strict-local
  8. fatal unknown date format unix
  9. fatal unknown date format iso 8601-strict
  10. fatal unknown date format
  11. git unknown date format unix
  12. java unknown date format
  13. unknown column 'date_format
  14. joi unknown date format
  15. opengrok unknown date format iso8601-strict
  16. apt unknown date format
  17. java parse unknown date format
  18. git log unknown date format
  19. unknown function 'apoc.date.format'
  20. git fatal unknown date format

Our application uses the Ext.grid.Panel to display rows of data. When the user clicks a "New..." button we are adding a new record to the grid store. Everything is working fine. However, on certain ...

Our application uses the Ext.grid.Panel to display rows of data. When the user clicks a "New..." button we are adding a new record to the grid store. Everything is working fine. However, on certain ...

I'm new to Javascript and wrote the code below to determine if a string is a palindrome. I'm curious as to what is the most efficient way to accomplish the same task. var isPalindrome = function (...

I'm new to Javascript and wrote the code below to determine if a string is a palindrome. I'm curious as to what is the most efficient way to accomplish the same task. var isPalindrome = function (...

  1. what is the stafford act
  2. what is a national emergency
  3. whatsapp
  4. what stocks to buy today
  5. whataburger
  6. what time is it
  7. what is my ip
  8. what song is this
  9. what is a state of emergency
  10. what is a dry cough
  11. what happens in a national emergency
  12. what does ���� mean
  13. what does a national emergency mean
  14. what to watch
  15. whataburger menu
  16. what is the weather
  17. what is rsv
  18. what is gluten
  19. what is pv
  20. what is a boomer

I want to run a function inside beforesend() in jquery ajax. Depending on the return value of that function I want to change the URL of ajax request. For an example refer below. If myFunction() ...

I want to run a function inside beforesend() in jquery ajax. Depending on the return value of that function I want to change the URL of ajax request. For an example refer below. If myFunction() ...

  1. ajax change function
  2. ajax change function jquery