function argument: jquery object passed as copy not reference?

function argument: jquery object passed as copy not reference?

Hello, I'm a little confused as to why a jquery group gets passed into a function as a copy not a reference, even though it is typeof 'object'. In this demo, it only works as I would expect in the second function ... Is there something special going on?

Tested in FF 3.6.9

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2.    "http://www.w3.org/TR/html4/loose.dtd">

  3. <html>
  4. <head>
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  6. </head>
  7. <body>
  8. <script>
  9. function test(arg){
  10. arg = arg.add($('<span/>'))
  11. console.log(arg);
  12. };
  13.       window.ele = $('<div/>');
  14. test(ele);   // both in the group get logged
  15. console.log(ele); // only the div
  16. function test2(arg){
  17. arg.a = arg.a.add($('<span/>'));
  18. console.log(arg.a);
  19. };
  20.       window.obj = {a:ele};
  21. test2(obj); // logs both
  22. console.log(obj.a); // logs both
  23. </script>
  24. </body>
  25. </html>