From: Jorge on
On Oct 23, 12:47 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> Jorge wrote:
> > Thomas 'PointedEars' Lahn wrote:
> >> (...)
> >> Well, let us assume for a brief moment that you would be right, that I
> >> would have missed the point, and that objects would be passed by
> >> reference in ECMAScript implementations. How do you explain then that
> >> any assignment to the argument named `a' does not modify the object
> >> "passed by reference" in the test case?
>
> > Because you're assigning a value to 'a', not to 'a'.something.
>
> But "pass by reference" means that the argument of the function is treated
> as a pointer to a memory area that the passed symbol represents.

Pointers are values. References are values. Reference is a more
generic term than pointer: pointers are just a certain kind of
reference. Sometimes references are pointers, sometimes they are
handles (a pointer to a pointer), sometimes they are the indexes of an
array, etc... and sometimes they are of the JS kind.

> By
> assigning to the argument, that memory area would be overwritten, and the
> value that the passed symbol represents would be changed.

No. That would happen when assigning to the result of de-referencing
the pointer. There's not an explicit de-reference operator in JS (C's
'*'). Nor the opposite "&".

> The test case for
> that, in PHP¹:
>
> <?php
> $bar = array('b');
>
> /* Array ( [0] => b ) */
> print_r($bar);
>
> /* &$a means pass-by-reference in PHP */
> function foo(&$a)
> {
> $a = array();
> }
>
> foo($bar);
>
> /* Array ( ) */
> print_r($bar);
> ?>

Here, inside foo, $a is a reference to the variable $bar, not a
reference to the array object ['b'] that $bar points to, therefore -
inside foo- $a= array(); is === $bar= array();

> Evidently that is not the case with
>
> var bar = ['b'];
>
> /* "b" */
> window.alert(bar);
>
> /* no pass-by-reference in ECMAScript implementations */
> function foo(a)
> {
> a = [];
> }
>
> foo(bar);
>
> /* "b" */
> window.alert(bar);

Here the value of 'a' -inside foo- is the same as the value of 'bar':
a reference to the object ['b'].
The difference with the PHP code above is obvious: 'a' isn't a
reference to 'bar', but to the object ['b'].

> so pass by reference simply does not happen there.

It does, only that the references do not point -see above- to the
things that you -mistakenly- thought.
--
Jorge.
From: John G Harris on
On Fri, 23 Oct 2009 at 12:47:05, in comp.lang.javascript, Thomas
'PointedEars' Lahn wrote:

<snip>
>But "pass by reference" means that the argument of the function is treated
>as a pointer to a memory area that the passed symbol represents. By
>assigning to the argument, that memory area would be overwritten, and the
>value that the passed symbol represents would be changed.
<snip>

But that depends on the semantics of the programming language. For
instance, in C/C++ if you have a pointer, p, then
p = ...
can make p point to something else, and
*p = ...
can replace the contents of the thing pointed to.

However, in C++ a reference, x, is more restricted.
x = ...
can replace the contents of the thing referenced, but there is no syntax
to make x reference something else.

On the other hand, in javascript an object 'reference', y, is the other
way round.
y = ...
can make y reference something else, but there is no syntax to replace
the contents of the thing referenced. However,
y.a = ...
is allowed and can do anything to a property of the thing referenced.

Your argument about the meaning of 'reference' is true for some
languages and false for others. Whether it is true or false for
ECMAScript is not something you will find it easy to prove by quoting
the standard.

John
--
John Harris
From: Lasse Reichstein Nielsen on
Thomas 'PointedEars' Lahn <PointedEars(a)web.de> writes:

> Jorge wrote:
>> Passed by reference means that you pass a reference: no
>> more, no less.

"Pass by reference" isn't really a fixed meaning in computer science.
It is likely to be taken to mean the same as "call-by-reference",
though (by people who know what that means: passing an L-value).

> And it does not apply here because "reference" has a specific meaning
> attached in general programming and so in the term "pass by reference".

"Reference" has many meanings. All you can really say from the word is
that it is something that references something (different from the
reference itself). It's also used in the ECMAScript text for something
completely third (and at no other point in this message am I referring
to that meaning of Reference).

>> More specifically, it means that what gets passed is not a copy of a
>> certain data (whatever the type of the data), but a reference to it.
>> It does not imply anything else, as you're pretending.

That's another common interpretation, matching the object passing
semantics of JavaScript (and Java).
I personally prefer the longer description: "Call-by-value with
reference types".

> And a reference as understood in "pass by reference" is implemented as a
> pointer to a memory location. A reference as understood in ECMAScript
> implementations is a reference to an object (necessarily stored at a memory
> location, but that is beside the point).
>
> As the graph showed, there can be any number of references to the same
> object; in this case, both `bar' and the initial value of a refer to the
> same object. If anything is copied here at all, it is the reference value.

Indeed. JavaScript is entirely call-by-value. However, Objects are not
values (they are neither expressible, nor denotable). Object references
are first class values, on the other hand.

This matches the Java and C# terminology of "Reference types" - types
that are not values, but where references to them are values.

>>> (... miss-the-point nonsense removed ...)
>
> Well, let us assume for a brief moment that you would be right, that I would
> have missed the point, and that objects would be passed by reference in
> ECMAScript implementations. How do you explain then that any assignment to
> the argument named `a' does not modify the object "passed by reference" in
> the test case?

Yes, it's definitly not call-by-reference.

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Jorge on
On Oct 24, 1:30 am, Lasse Reichstein Nielsen <lrn.unr...(a)gmail.com>
wrote:
> Thomas 'PointedEars' Lahn <PointedE...(a)web.de> writes:
>
> > Jorge wrote:
> >> Passed by reference means that you pass a reference: no
> >> more, no less.
>
> "Pass by reference" isn't really a fixed meaning in computer science.
> It is likely to be taken to mean the same as "call-by-reference",
> though (by people who know what that means: passing an L-value).
>
> > And it does not apply here because "reference" has a specific meaning
> > attached in general programming and so in the term "pass by reference".
>
> "Reference" has many meanings. All you can really say from the word is
> that it is something that references something (different from the
> reference itself). It's also used in the ECMAScript text for something
> completely third (and at no other point in this message am I referring
> to that meaning of Reference).
>
> >> More specifically, it means that what gets passed is not a copy of a
> >> certain data (whatever the type of the data), but a reference to it.
> >> It does not imply anything else, as you're pretending.
>
> That's another common interpretation, matching the object passing
> semantics of JavaScript (and Java).
> I personally prefer the longer description: "Call-by-value with
> reference types".
>
> > And a reference as understood in "pass by reference" is implemented as a
> > pointer to a memory location.  A reference as understood in ECMAScript
> > implementations is a reference to an object (necessarily stored at a memory
> > location, but that is beside the point).
>
> > As the graph showed, there can be any number of references to the same
> > object; in this case, both `bar' and the initial value of a refer to the
> > same object.  If anything is copied here at all, it is the reference value.
>
> Indeed. JavaScript is entirely call-by-value. However, Objects are not
> values (they are neither expressible, nor denotable). Object references
> are first class values, on the other hand.

Aha !

> This matches the Java and C# terminology of "Reference types" - types
> that are not values, but where references to them are values.
>
> >>> (... miss-the-point nonsense removed ...)
>
> > Well, let us assume for a brief moment that you would be right, that I would
> > have missed the point, and that objects would be passed by reference in
> > ECMAScript implementations.  How do you explain then that any assignment to
> > the argument named `a' does not modify the object "passed by reference" in
> > the test case?
>
> Yes, it's definitly not call-by-reference.

Yes it is -effectively- when the argument is an object: call-by-
reference === call-by-value where the value is a reference.
--
Jorge.
From: VK on
Thomas 'PointedEars' Lahn writes:
> > Well, let us assume for a brief moment that you would be right, that I would
> > have missed the point, and that objects would be passed by reference in
> > ECMAScript implementations.  How do you explain then that any assignment to
> > the argument named `a' does not modify the object "passed by reference" in
> > the test case?
Lasse Reichstein Nielsen wrote:
> Yes, it's definitly not call-by-reference.

My two cents:
JavaScript doesn't allow dereferencing and reference value access
which makes some explanations maybe too "theoretical" without a
possibility to visualize them. Perl is no way a JavaScript close
relative but its internal referencing schema is pretty close:

#!/usr/bin/perl

# Creating an anonymous array and
# assigning a reference to it to $arr
$arr = [1, 2, 3, 4, 5];

# What is printed is that "reference value"
# discussed here. It is just a string of
# a strictly defined internal format
# starting with the object id and
# followed by memory coords where the
# array first element is located.
print $arr; # "ARRAY(0x365040)"
print "\n";

# Yes, it is a reference to
# an object of type Array,
# just double checking :)
print ref $arr; # "ARRAY"
print "\n";

# Assigning reference value
# to another var. Technically
# it is absolutely the same as
# $a = "abc"; $b = $a
$alias = $arr;
print $alias; # ARRAY(0x65040)
print "\n";

# Technically we are just
# replacing "ARRAY(0x365040)"
# with "foo"
$arr = "foo";
print $arr; # foo
print "\n";

# Dereferencing(!) the
# reference value so telling
# to the system that we don't
# need "ARRAY(0x365040)" string,
# we need the array by that ref.
print @$alias; # 12345