Guest User

Untitled

a guest
Jan 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. var y;
  2. foo();
  3. // values: 3 10
  4. // value: 4 10
  5.  
  6. // *******************************
  7.  
  8. function foo() {
  9. for (let i=0; i<2; i++) {
  10. let obj = { x: 2 }; // <-- bug because of `let` here
  11. bar(obj);
  12.  
  13. setTimeout(function(){
  14. obj.x = 10;
  15. },100);
  16. }
  17. }
  18.  
  19. function bar(obj) {
  20. setTimeout(function(){
  21. y.x++;
  22. console.log(`values: ${y.x} ${obj.x}`);
  23. },1000);
  24.  
  25. y = Object.assign({},obj);
  26. }
Add Comment
Please, Sign In to add comment