SHARE
TWEET

Untitled

a guest Mar 26th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Misunderstanding scope:thinking that block-level scope exist here
  2. var array = [];
  3. for (var i = 0; i < 3; i++) {
  4.   // Every 'i' in the bodies of the three arrow functions referes to the same binding,
  5.   // which is why they all return the same value of '3' at the end of the loop.
  6.   array.push(() => i);
  7. }
  8. var newArray = array.map(el => el());
  9. console.log(newArray); // [3, 3, 3]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top