Reference

Objects are never copied. They are passed around by reference.

  1. // Imagine I had a pizza
  2. var myPizza = { slices: 5 };
  3. // And I shared it with You
  4. var yourPizza = myPizza;
  5. // I eat another slice
  6. myPizza.slices = myPizza.slices - 1;
  7. var numberOfSlicesLeft = yourPizza.slices;
  8. // Now We have 4 slices because myPizza and yourPizza
  9. // reference to the same pizza object.
  10. var a = {},
  11. b = {},
  12. c = {};
  13. // a, b, and c each refer to a
  14. // different empty object
  15. a = b = c = {};
  16. // a, b, and c all refer to
  17. // the same empty object