Guest User

Untitled

a guest
Feb 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. function Reducer(counter : number = 0, action : Action) {
  2. counter++; // Should trigger an error but it doesn't.
  3. return counter + 1;
  4. }
  5.  
  6. function NestedReducer(nestedCounter : { counter : number } = { counter : 0 }, action : Action) {
  7. nestedCounter = { counter : 0 }; // Doesn't trigger an error.
  8. nestedCounter.counter++; // This does trigger an error.
  9. return { counter : nestedCounter.counter + 1 };
  10. }
  11.  
  12. let a = { counter : 0 };
  13. let b = a;
  14. console.log(`${a.counter}, ${b.counter}`);
  15. b.counter++; // This changes a.counter as well since both a and b reference the same object.
  16. console.log(`${a.counter}, ${b.counter}`);
  17. b = { counter : 0 }; // This does not change a or a.counter, now a and b point to different objects.
  18. console.log(`${a.counter}, ${b.counter}`);
  19.  
  20. 0, 0
  21. 1, 1
  22. 1, 0
Add Comment
Please, Sign In to add comment