daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const a = {
  2.     x: 0
  3. };
  4. const f = function(a) {
  5.     a.x = 100;
  6. }
  7. f(a);
  8. console.log(a);
  9. // will print { x: 100 }
  10.  
  11. // =================================
  12.  
  13. const a = {
  14.     x:0
  15. };
  16. const f = function(a) {
  17.     a = 'foo';
  18. }
  19. f(a);
  20. console.log(a);
  21. // will print { x: 0 }
  22.  
  23.  
  24. You're talking about calling a function "by reference". JavaScript (like most other functional languages) doesn't support that, as, changing a variable that is outside the current scope is a side-effect and those contradict functional programming paradigms.
  25. You can always change variables inside the current scope though. So any function defined within another function can change any local variable of the outer one.
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