From: Ry Nohryb on
On May 19, 3:14 am, Stefan Weiss <krewech...(a)gmail.com> wrote:
> On 18/05/10 15:05, 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) ?

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 can't avoid
to pass objects by reference.
--
Jorge.
From: Thomas 'PointedEars' Lahn on
RobG wrote:

> Thomas 'PointedEars' Lahn wrote:
>> RobG wrote:
>> > williamc wrote:
>> >> Thomas 'PointedEars' Lahn wrote:
>> >> > RobG wrote:
>> >> >> williamc wrote:
>> >> >> [...]
>> >> >>> The second function creates the desired array of functions. Zakas:
>> >> >>> "The anonymous function has one argument, num, which is the number
>> >> >>> that the result function should return. Since function arguments
>> >> >>> are passed by value, the current value of i is copied into the
>> >> >>> argument num."
>> >> >> More misdirection. They are always references,
>> >> > What do you mean by "they" here?
>> > The values that are assigned by the calling function.
>> How can *you* know for sure what *he* meant?
>
> You asked what *I* meant, I've explained that.

Right. Sorry. But in that case, you are dead wrong.

> [...]
>> > I think in both cases their values are references because, to me, it
>> > is less confusing. As I posted eariler, given:
>>
>> > var a = 5,
>> > b = a;
>>
>> > You can think of a and b as refering to the same instance of 5.
>>
>> But you should not. While Smalltalk undoubtedly is an ancestor of
>> JavaScript/ECMAScript, by contrast `5' is _not_ an object in ECMAScript
>> implementations (it is only being implicitly converted to a Number
>> instance sometimes).
>>
>> So, in your words, `b' "refers" to _another_ `5' than `a'.
>
> No, in my words, 'b' and 'a' refer to the same '5'. Since that '5'
> can't be modified, the value of 'b' can only be changed by assigning a
> new value, even if it's another '5'.

That's nonsense. Have you subscribed to VK101 by now?

>> Indeed, the variables `a' and `b' would very likely be implemented as two
>> pointers pointing to different heap addresses.
>
> Maybe, but implementation details are irrelevant.

No, implementation details are at the core of the issue.

>> > Anyhow, that's my way of understanding it.
>> Well, misconceptions are quite common in this field.
>
> Apparently - I'll keep this one to myself. BTW, do you have a link to
> where it was discussed here previously?

I might have a Message-ID.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
From: Thomas 'PointedEars' Lahn on
Garrett Smith wrote:

> Stefan Weiss wrote:
>> On 18/05/10 15:05, 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. Incidentally,
>> that's also the way it works in Java, and Java is also the source of the
>> expression "reference types" that has been thrown around here a couple
>> of times.
>
> The same behavior is seen in Java, however, Java's "reference types" has
> different meaning that Reference type in ECMAScript.
>
> Java Reference Types, from JLS, 3rd Edition, Chapter 4:
> | The reference types (§4.3) are class types, interface types, and
> | array types.
> http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html
>
>
> ECMAScript Reference Type, from ECMAScript Edition 3, s 8.7
> | The internal Reference type is not a language data type. It is defined
> | by this specification purely for expository purposes. An
> | implementation of ECMAScript must behave as if it produced and
> | operated upon references in the manner described here. However, a
> | value of type Reference is used only as an intermediate result of
> | expression evaluation and cannot be stored as the value of a variable
> | or property.
>
> Elsewhere:
> http://dmitrysoshnikov.com/ecmascript/chapter-8-evaluation-strategy/

And neither one has anything to do with reference values, as established
recently.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: Ry Nohryb on
On May 19, 2:41 pm, Tim Streater <timstrea...(a)waitrose.com> wrote:
> In article
> <65fb1f2e-92e3-460a-99f3-b550aefc3...(a)40g2000vbr.googlegroups.com>,
>  Ry Nohryb <jo...(a)jorgechamorro.com> wrote:
>
>
>
>
>
> > On May 19, 3:14 am, Stefan Weiss <krewech...(a)gmail.com> wrote:
> > > On 18/05/10 15:05, 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) ?
>
> > 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 can't avoid
> > to pass objects by reference.
>
> You are not passing the object, you are passing a *pointer* to the
> object. When you pass this *pointer*, the function makes its own copy of
> the *pointer* and the original *pointer* is not modified with the test
> function.
>
> Which is why:
>
>   function test (o) { o = null; }
>
> does *not* change p, whereas:
>
>   function test (o) { o.j= "Simpson"; }
>
> *does* change the *object* that p points at. While test is running,
> there are two pointers to the object, and only one before and after (or
> at some point after, possibly).

Exactly. test(p) in JS is exactly as test(&p) in C : a pointer = a
reference.
--
Jorge.
From: Thomas 'PointedEars' Lahn on
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?

/* 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).

Therefore, the term "(object) reference (value)" is being used to describe
what happens instead.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)