Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  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]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement