Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Misunderstanding scope:thinking that block-level scope exist here
- var array = [];
- for (var i = 0; i < 3; i++) {
- // Every 'i' in the bodies of the three arrow functions referes to the same binding,
- // which is why they all return the same value of '3' at the end of the loop.
- array.push(() => i);
- }
- var newArray = array.map(el => el());
- console.log(newArray); // [3, 3, 3]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement