On May 19, 3:15*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> Tim Streater wrote:
> > Ry Nohryb wrote:
> >> Stefan Weiss wrote:
> >> > David Mark wrote:
> >> > > The value passed is a reference to an object. That might sound
> >> > > like a contradiction in terms, but consider:-
>
> >> > > function test(o) {
> >> > > o = null;
> >> > > }
>
> >> > > var p = {};
> >> > > test(p);
> >> > > window.alert(p); // Not null
>
> >> > I think that's a perfect illustration. It's all you need to see to
> >> > understand that there is no pass-by-reference in JS. (...)
>
> >> The same example slightly modified can be used to show that it's a
> >> pass-by-reference:
>
> >> function test (o) { o.j= "Simpson" }
> >> var p= {};
> >> test(p);
> >> p.j
> >> --> "Simpson"
>
> >> The question is, when you say "it's been passed by reference", what
> >> exactly did you expect *it* to be ?
>
> >> 1.- var p ?
> >> 2.- the value of var p (which is a reference to an object) ?
>
> 1. *But that is _not_ what happens. *This has nothing to do with variables,
> as you will see shortly.
>
> >> And the answer is in the ES3 specs, 10.1.8, "The initial value (... of
> >> an argument ...) is *the*value* of the corresponding actual parameter
> >> supplied by the caller."
>
> >> So, while in JS you can't pass a reference to a var,
>
> You cannot *pass* something (like a reference) to something else that cannot
> be called (like a variable). *That much is true.
>
> >> you can't avoid to pass objects by reference.
>
> No, you pass the reference value to the function (by value). *How many times
> has that been explained to you already, Jorge Chamorro, albeit your not
> using a pseudonym back then?
"PointedEars" is to Thomas Lahn what "Ry Nohryb" ( === rot13("El
Abuelo") ) is to Jorge Chamorro: a nick name (not a pseudo-name). My
name is as clear as an unmuddied lake, Fred, as clear as an azure sky
of deepest summer, Fred, you can rely on me, in both in the from
header of my posts and in the signature.
> * /* Firebug 1.5.4: Object { j="Simpson"} */
> * (function (o) { o.j = "Simpson"; return o; }({}))
>
> > You are not passing the object, you are passing a *pointer* to the
> > object.
>
> No, think twice. *If it was a pointer, which it is not, it would work like
> call-by-reference: it would be possible to overwrite the object by assigning
> to (the identifier of) the argument (since this would require implicit
> pointer referencing we could reasonably assume implicit pointer
> dereferencing).
To begin with pointers and C are at a much lower level than JS. In C,
a pointer is a reference to a memory location, and C -being a lower
lever language- allows you to write there whatever you want, and, in
fact, to read what is there and cast it into whatever -any other- data
type you wish, unlike in JS. E.g. you can write a struct (an object)
there and read it back as a char * or a longint, if you wish, or
viceversa.
In JS, the object that the reference points to can be overwritten, and
the reference itself, too, but you can't cast the reference to a
different data type: IOW, given a reference to an object, you can't
make it be a different thing (by casting). And in the same way that a
given memory address (pointer value) always points to the same logical
memory address, a given reference (in JS) can not be made to point to
a different object.
Thinking in terms of pointers, when in C you've got 2 different copies
of the same pointer (pointers in C, as references in JS, are passed by
copy), as when in JS you've got 2 different references to the same
object, assigning a different value to any of the pointers won't
change the other pointer, exactly as what happens in JS.
My point since day 1, is that an object is said to be passed-by-
reference when instead of copying it (as in pass-by-copy), what gets
passed is a *reference* that points to it, as is the case in JS.
You insist in that in JS the reference is a value that gets passed by
copy, and I insist in that that's exactly what C does when passing an
objectOrWhatever by reference as in functionToCall(&objectOrWhatever).
You insist in that that's not to pass by reference, and I insist in
that that's been being called so among C programmers for nearly 3
decades now.
> (...)
--
Jorge.