1.值传递

  1. <script>
  2. var a = 1;
  3. var b = 2;
  4. function changeA(a,b){
  5. var tmp = a;
  6. a = b;
  7. b = tmp;
  8. }
  9. changeA(a,b);
  10. console.log(a,b);
  11. </script>

image.png

2.引用传递 function 和 Object

  1. <script>
  2. //Array 是Object
  3. var a = [1,2];
  4. function changeB(a){
  5. var tmp = a[0];
  6. a[0] = a[1];
  7. a[1] = tmp;
  8. }
  9. changeB(a);
  10. console.log(a);
  11. </script>

image.png