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
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- </head>
- <body>
- <script>
-
- function test(arg){
- arg = arg.add($('<span/>'))
- console.log(arg);
- };
-
-
window.ele = $('<div/>');
- test(ele);
// both in the group get logged
- console.log(ele); // only the div
-
- function test2(arg){
- arg.a = arg.a.add($('<span/>'));
- console.log(arg.a);
- };
-
-
window.obj = {a:ele};
- test2(obj); // logs both
- console.log(obj.a); // logs both
-
- </script>
- </body>
- </html>