karlakmkj

Object - pass by reference

Dec 15th, 2020 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //create spaceship object
  2. let spaceship = {
  3.   'Fuel Type' : 'Turbo Fuel',
  4.   homePlanet : 'Earth'
  5. };
  6.  
  7. //Function that has object as a parameter that sets the property of 'Fuel Type'
  8. let greenEnergy = obj => obj['Fuel Type'] = 'avocado oil';
  9.  
  10. //Function that sets/reassigns object as a parameter
  11. let remotelyDisable = obj => obj.disabled = true;
  12.  
  13. //invoke the function and pass spaceship as the object
  14. greenEnergy(spaceship);
  15. remotelyDisable(spaceship);
  16.  
  17. //print spaceship to confirm the changes in the properties  
  18. console.log(spaceship);
  19.  
  20. console.log(spaceship['Fuel Type']);
  21. console.log(spaceship.disabled); //disabled property was added
Add Comment
Please, Sign In to add comment