4 ms·
JavaScript Closure Stories part III
- kelmop 13y agoI understand first and second example, but not the third. Someone care to explain? You can assign value for card but no read at the same line? Once I though that I understand something but I was wrong :)
- nadinengland 13y agoClosure allows you to capture references to variables, not objects. The function may reference the variable `card` but by the time the first one is triggered the for-loop had already decremented the variable to 0.
- kelmop 13y agoThanks! I think that I will stick with this: "You should stick with the simple stuff from now on. Capisce?"
- actsasbuffoon 13y agoIf you use Array.map (or underscore's map, etc.) then you won't run into this problem. The functional programmer in me gets a little angry whenever I see a for loop in a language that supports higher order functions.
- malandrew 13y agoI just wish JavaScript map was more powerful. I like that underscore's map provides a bit more information passed to the mapped function. It would also be nice if there was an object map function built into the language as well. Again underscore has a solution but the core prototypical functions lack here. It would be awesome if many of the solutions in underscore and other high quality functional libraries could be added to the native prototypes.