Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript the function return 10 always why?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #145054
    omryk
    Participant

    i have this code why it return 10 always?

    function createFunctions() {
    var result = new Array();

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

    }

    #145189
    dgriesel
    Participant

    If you are interested why that is happening, you should definitely have a look at this talk by Douglas Crockford: http://www.youtube.com/watch?v=ya4UHuXNygM&list=PL7664379246A246CB

    Your problem comes up at 1h:06m, but I would really recommend watching the rest of the video – maybe even the whole series of 8 videos!

    #145193
    dgriesel
    Participant

    @CrocoDillon: I’m not saying that your answer is not sufficient, but the question reminded me of seeing this talk which I found incredibly helpful and interesting ;)

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.