This leeds me to believe that the line
elm.onclick = thisObj.onclick;
is copied by value and NOT by reference.
Nothing is actually "passed by reference"--everything is passed by value. The only question is whether you are passing an address by value or the whole object by value, i.e. whether only an address is being copied or the whole object is being copied.
A reference is actually the "address" in memory where an object is located. An address looks something like this:
456AF213
Don't be frightened by that. It's just a number, actually a simple integer, and it indicates where the object resides in memory. Now, as to why that is relevant. This is what you start out doing:
var RefA = obj;
var RefB = RefA;
The issue you are wrestling with is: does RefB refer to RefA or directly to obj? You started out believing RefB refers to RefA, and therefore any change to RefA would be reflected in RefB. However, that is incorrect. RefB refers directly to obj, and by examining what is happening with the "address", you can see why. If obj is at some location in memory signified by:
456AF213
then when you assign obj to RefA:
var RefA = obj;
the address is assigned to the variable RefA, and you end up with this:
RefA = 456AF213
Substituting for RefA in the second assignment:
var RefB = RefA;
gives you:
var RefB = 456AF213;
The assignment just copies the address stored in RefA to RefB. Subsequently, if you change obj in some way, then both RefA and RefB will reflect that change--revealing that they are in fact references. For instance:
obj.new_property = "red";
alert(RefA.new_property); //"red"
alert(RefB.new_property); //"red"
However, if you change RefA to refer to some other object, it has no effect on RefB. For instance:
RefA = another_obj;
Since another_obj will be at a unique place in memory, objRef will get assigned a new "address":
RefA = 1234AE24
However, that assignment in no way affects RefB, and RefB will still refer to obj.
That may be explanation enough for you. However, if you still have some lingering suspicions, read on. The crux of your dilemma is that there is a fundamental difference in the two assignments:
var RefA = obj;
var RefB = RefA;
This is the situation in memory just before the second assignment:
obj RefA=[COLOR=Red]456AF213[/COLOR] RefB
--------- ---------- ----------
[COLOR=Red]456AF213 [/COLOR] 1344AC45 9245AD23
In the first assignment:
var RefA = obj;
the assignment works this way: js takes the address of what's on the right and assigns it to the variable on the left:
var RefA <---- address of obj
In the second assignment:
var RefB = RefA;
if the assignment worked the same way, then the address of what's on the right, i.e. RefA, would be assigned to RefB. RefA is a separate variable in its own right, and therefore it has its own address, as shown in the diagram. If the second assignment worked the same way, the result would be:
obj RefA=[COLOR=Blue]456AF213 [/COLOR] RefB=[COLOR=Red]1344AC45[/COLOR]
--------- ---------- ----------
[COLOR=Blue]456AF213[/COLOR] [COLOR=Red]1344AC45 [/COLOR] 9245AD23
and then any changes to RefA would also affect RefB because RefB would be a reference to RefA. However, before the assignment takes place, js examines what is on the right side of the equals sign. If to the right of the equals sign there is an object, e.g. a function, then js takes the address of that object and assigns it to the variable on the left of the equals sign. However, if js finds a reference on the right side of the equals sign, then js just copies the address stored in the reference and assigns it to the variable on the left.
or
?