Gotcha (programming) - Gotchas in JavaScript Programming Language

Gotchas in JavaScript Programming Language

JavaScript function closures inside loops don’t work intuitively.

var func = ; for(var i = 0; i < 3; i++) { func = function { alert(i); } } func; func;

correct way:

var func = ; for(var i = 0; i < 3; i++) { func = (function(i){ return function{ alert(i); } })(i); //HERE IT IS } func; func;

Read more about this topic:  Gotcha (programming)

Famous quotes containing the words programming and/or language:

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)

    Which I wish to remark—
    And my language is plain—
    That for ways that are dark
    And for tricks that are vain,
    The heathen Chinee is peculiar:
    Which the same I would rise to explain.
    Bret Harte (1836–1902)