Thomas said:
Richard Cornford wrote:
No.
You mean they are passed by value? They are not.
The value of an object is the totality of its state, so languages that
pass objects by value create a snapshot clone of the object and make
that available to the function. Javascript never does that, not even
with functions as previously suggested in this thread.
The known pass-by-* scheme of programming languages
implementing pointers does not apply here.
What is the point of stating that something that wasn't mentioned and
doesn't apply is irrelevant?
A JS reference is not a pointer.
Nobody has proposed that a reference is a pointer. Nobody but you has
even mentioned pointers. That the mechanism of how an object is refereed
to by a value assigned as an object property is left to the language
implementer is irrelevant. We know that many object properties may refer
to the same object instance. We can call that a reference and never
concern ourselves with the internal details.
Objects in JS are only available via an object reference.
So they can only be available inside a function to which they are
presented as an argument as a reference to an object.
One could say that those references are passed to
methods by value
Yes you could. The odds are very good that a value that represents some
sort of reference to an object is actually copied in the process of
passing a reference to an object as a function argument. Thus the
reference itself is passed by value, but passing the value of a
reference is equivalent to passing the object referred to by reference
(the copy of the value of the reference will still refer to the same
object instance).
and that value is the object.
You would be better off saying that the value of a reference to an
object is a reference to an object.
Thinking in terms of the value of an object property that refers to an
object as being that object is going to cause confusion as soon as it is
apparent that many properties refer to the same object and so that many
values in diverse locations are all that one object (just writing it
down makes it a self-evident that it is confusing).
A great deal of what makes OO useful is that it renders abstract notions
tangible. The very use of the word 'Object' to describe something so
nebulous that (in javascript at least) we don't even care how it
manifests itself inside the computer is an indication of that. An object
instance is just that; one entity, which is created, has a life span and
is disposed off (in some way). It makes much more sense to think of many
values referring to that one object than of many values being that one
object.
If you see it this way, it is quite clear why
function foo(x, y)
{
So when execution reaches this point the 'x' property of the function's
execution context's activation/Variable object holds a value that is a
reference to the object that is also referred to by the value of the
global 'y' variable and its 'y' property refers to the same object as
the object referred to by the global 'z' variable.
And now the 'x' property of the Activation/Variable object has been
assigned a reference to a new object.
While the reference held in the 'y' property has been used to identify
the object that is also referred to by the global 'z' variable and
assign a new value to one of its properties.
}
var y = {}, z = {};
foo(y, z);
does not result in (pseudocode)
y == {bar: 42, foobar: 23}
There was no reason to ever expect that it would. Assuming you are
referring to the object referred to by the global 'y' variable then no
operations have been performed that could influence it.
but in
y == {}
z == {foobar: 23}
The value of the first reference variable is changed to be
assigned a reference to a *new* object,
I assume you mean the first reference _parameter_ here, as it is the
value of the parameter (as manifest in the corresponding named property
of the Activation/variable object) that was modified to be a reference
to a new object.
and the value of the second reference variable (i.e.
the object) is changed to have a new property.
The state of the object was changed, indirectly, via the reference to
the object passed to the function as its second parameter.
It is significant that even imprecisely worded you are forced to express
the behaviour of the code in terms of references to objects. In
javascript all objects are passed by reference.
Richard.