From: Jorge on
On Oct 22, 11:16 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
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.
--
Jorge.
From: optimistx on

"Johannes Baagoe" <baagoe(a)baagoe.com> kirjoitti
viestissä:nqqdnfXOXbMbCELXnZ2dnUVZ8u6dnZ2d(a)giganews.com...

> It shows that contrary to what the distinction between "call by value"
> for primitive values and "call by reference" for object implies,
> arguments of any type are actually passed the same way.
>
> No reassignment to the argument inside the function will be visible
> outside the function.
>
> Modifications inside the function of an argument's *properties*
> will, however, and this obviously only applies to objects, since only
> objects have properties. (Arrays, etc, are special cases of objects.)
>
> And once the argument has been overwritten inside the function,
> any subsequent modification of its properties (if there are any)
> will not be visible outside the function.
>
> It is quite simple, actually, it only has to be formulated clearly,
> perhaps with a few examples, and preferably without too much jargon.
>
Yes, WITHOUT too much jargon :). That helps in understanding
prototype chains, 'this', function arguments passing. A couple of
pictures would be a start. I might draw next week, if somebody else
has not done it by then.


Inroducing jargon gives me the feeling like
this:
A: 'I plan to by a new car with about 15 000 euros. Recommendations?'.
B: 'No you do not.'
A: 'Why not, I know what I want'.
B: 'No you stupid idiot, RTFM, there is no car costing 15 000 euros'
A:'I saw one with the price 14 900 euros in the price tag'
B: 'That is not 15000 euros, you clueless newbie, go away'
C: 'According to the ECMA-car-bying standards manual, you
do not qualifyi as a -buyer- , if your whole address is not
visible on the attrition line. Further, the standard does not
recocnize the word 'car', I gues you might , or you might not,
mean 'Personal Vehicle for Transportation of Human Beings',
blah blah blah
I do not want waste my time with such sillyness.
A. 'Sigh'
etc etc

From: Richard Cornford on
optimistx wrote:
<snip>
> Inroducing jargon gives me the feeling like
> this:
> A: 'I plan to by a new car with about 15 000 euros. Recommendations?'.
<snip>
> blah blah blah
> I do not want waste my time with such sillyness.
<snip>

You mean when that time could be better wasted in pointless whining?

Richard.

From: optimistx on
Richard Cornford wrote:

> You mean when that time could be better wasted in pointless whining?
>
> Richard.

Have you ceased to beat your wife, yes or no?
From: Thomas 'PointedEars' Lahn on
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. By
assigning to the argument, that memory area would be overwritten, and the
value that the passed symbol represents would be changed. 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);
?>

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);

so pass by reference simply does not happen there.

What is overwritten instead is the *value* that the argument `a' holds, the
value that is passed to the function. That value can be either a primitive
value or a reference(-to-an-object) value; it does not really matter for the
overwriting. In any case, it is a *value*, and it is passed as such (by
*value*).

As a result, when the passed *value* is a reference(-to-an-object) *value*,
when used with property accessor syntax, the argument identifier evaluates
to the object that is being referred to by the reference(to-an-object)
*value*.

var bar = ['b'];

/* "b" */
window.alert(bar);

function foo(a)
{
/* `a' stores a reference to the same object as `bar' */
a[0] = 42;

/*
* `a' no longer stores a reference to the same object as `bar'
* → this change is not visible outside
*/
a = [];

/* this change is not visible outside either */
a[0] = 23;
}

foo(bar);

/* "42" */
window.alert(bar);

If that does not convince you that you are mistaken, I do not know what can.


PointedEars
___________
¹ <http://php.net/manual/en/language.references.pass.php>
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)