For-loop saving state with closure
问题 Forgive me if this might be a bit of a noobie question, but this should work shouldn't it? var elems = [1,2,3,4,5] for (var i = 0; i <elems.length; i++) { return (function(e){ console.log(e) })(i); } Meaning, it should spit out >>node file.js 1 2 3 4 5 For some reason this isn't doing this. Rather when it is run in terminal, it spits out >>node file.js 1 What am I missing? Could you please elaborate. 回答1: Because you are returning the value returned by the IIFE immediately, in this statement