From: williamc on
On 5/18/2010 6:52 AM, Thomas 'PointedEars' Lahn wrote:
> RobG wrote:
>
>> On May 18, 3:59 am, williamc <n...(a)nowhere.net> 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?
>
>> but the *value* might be a primitive or an object.
>
> No, the value can be a primitive value or an object reference.
> You cannot access an object directly, you need a reference to it.
> As a result, there can be multiple references to the same object.
> The object may be subject to garbage collection if it is no longer
> referred, i.e. there are no more references to it (much like with
> hardlinks).
>
>> The the concept that "function arguments are passed by value" might be
>> kind of true if function arguments are always primitives, but they
>> aren't.
>
> Zakas' premise is right here; you are not. Object references are values.
> See also
> <https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Functions>,
> which was agreed on here.
>

I wondered what people in c.l.j would say about the Zakas' point that
function arguments are always passed by value. He kind of stresses it,
in the sense that he repeats it several times. I didn't really feel
comfortable with it at the time, and I still don't feel comfortable with
it. In most languages doesn't the concept of pass by value vs. pass by
reference refer to passing an independent copy vs. passing a reference
to the thing itself?

I have a "something I'm missing here" feeling...

obj = {
propX: "foo"
};
num = 27;

function test(argNum, argObj) {
argNum = 28;
argObj.propX = "bar";
}

test(num, obj);

console.log(num); // still 27
console.log(obj.propX); // "bar", so why isn't this called a pass
by reference?

In the link Thomas cited it says...

"The parameters of a function call are the function's arguments.
Arguments are passed to functions by value. If the function changes the
value of an argument, this change is not reflected globally or in the
calling function. However, object references are values, too, and they
are special: if the function changes the referred object's properties,
that change is visible outside the function, ... "

So, I guess I'm having a hard time with the "they are special". Wouldn't
this specialness simply be called a pass by reference in most languages?

--williamc







> PointedEars

From: David Mark on
williamc wrote:
> On 5/18/2010 6:52 AM, Thomas 'PointedEars' Lahn wrote:
>> RobG wrote:
>>
>>> On May 18, 3:59 am, williamc <n...(a)nowhere.net> 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?
>>
>>> but the *value* might be a primitive or an object.
>> No, the value can be a primitive value or an object reference.
>> You cannot access an object directly, you need a reference to it.
>> As a result, there can be multiple references to the same object.
>> The object may be subject to garbage collection if it is no longer
>> referred, i.e. there are no more references to it (much like with
>> hardlinks).
>>
>>> The the concept that "function arguments are passed by value" might be
>>> kind of true if function arguments are always primitives, but they
>>> aren't.
>> Zakas' premise is right here; you are not. Object references are values.
>> See also
>> <https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Functions>,
>> which was agreed on here.
>>
>
> I wondered what people in c.l.j would say about the Zakas' point that
> function arguments are always passed by value. He kind of stresses it,
> in the sense that he repeats it several times. I didn't really feel
> comfortable with it at the time, and I still don't feel comfortable with
> it. In most languages doesn't the concept of pass by value vs. pass by
> reference refer to passing an independent copy vs. passing a reference
> to the thing itself?
>
> I have a "something I'm missing here" feeling...
>
> obj = {
> propX: "foo"
> };
> num = 27;
>
> function test(argNum, argObj) {
> argNum = 28;
> argObj.propX = "bar";
> }
>
> test(num, obj);
>
> console.log(num); // still 27
> console.log(obj.propX); // "bar", so why isn't this called a pass
> by reference?
>
> In the link Thomas cited it says...
>
> "The parameters of a function call are the function's arguments.
> Arguments are passed to functions by value. If the function changes the
> value of an argument, this change is not reflected globally or in the
> calling function. However, object references are values, too, and they
> are special: if the function changes the referred object's properties,
> that change is visible outside the function, ... "
>
> So, I guess I'm having a hard time with the "they are special". Wouldn't
> this specialness simply be called a pass by reference in most languages?
>

No. 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
From: Stefan Weiss on
On 18/05/10 13:26, Thomas 'PointedEars' Lahn wrote:
> Stefan Weiss wrote:
>
>> Eleandor wrote:
>>> Person = (function() {
>>> return function() {
>>> var self = this;
>> ...
>>
>> Minor nitpick: self is not a good variable name in browser scripting;
>> it's usually an alias for the window object. It won't cause a problem in
>> your example, but it could confuse people who see a call to
>> "self.shout()" before they see the assignment above.
>
> It is not "usually an alias for the window object", it is a property of
> Window instances or their prototype to refer to the instance. A Window
> instance's prototype may be in the prototype chain of the ECMAScript Global
> Object (this is the case with client-side JavaScript 1.8.2 in Gecko
> 1.9.2.3).

Very verbose, but technically correct. That doesn't affect the point I'm
trying to make, however: in browser scripting, the "self" identifier
usually already refers to _something_ - just like "document" and
"window" do. You're free to assign something else to them, but this can
cause confusion for readers.

> Since to my knowledge it is unnecessary to access the `self' property of
> Window instances, I can see no cause for confusion here. Indeed, the
> declaration follows the pattern of several other OOPLs, including most
> notably, IIUC, Smalltalk which influenced the prototype-based Self
> programming language (where you can omit `self') which influenced JavaScript
> (unfortunately, Brendan Eich apparently did not see the importance of the
> `self' keyword in JavaScript's predecessors).

Neither do I. As a reference to the current context (or instance),
"self" feels more natural to me than "that" or "me" (some conventional
alternatives), but reusing it has the potential for confusion and should
therefore be avoided, IMHO.

For example: I didn't read the original code posted by Eleandor, mostly
because of the formatting. The first thing I saw was Matt's example:

elm.onclick = (function(inner_i) {
return function() {
self.shout(inner_i);
}
})(i);

I wondered why he would call shout() as self.shout(), so I checked
whether self had been redefined (it was), but from that snippet alone,
it looked like shout() was a global function.


--
stefan
From: williamc on
On 5/18/2010 9:05 AM, David Mark wrote:

> No. 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

Yikes! Well, that's certainly a good example, as I would have expected p
to be null after the function call. But I can't say that I've succeeded
in understanding why it isn't.

So, the right way to think about is the object "supplies the value to
the function"?

From: Thomas 'PointedEars' Lahn on
williamc wrote:

> David Mark wrote:
>> No. 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
>
> Yikes! Well, that's certainly a good example, as I would have expected p
> to be null after the function call. But I can't say that I've succeeded
> in understanding why it isn't.

As `o' is but a variable (sort of) that is assigned a value, a reference to
an object, as execution enters the context of the function [ES3/5, 10.4] --

p
||
ref ---> [object Object]
||
o

-- then changing the value of the (local) "variable" in the function does
not change the object, of course:

p
||
ref ---> [object Object]

o = null

That is where object references are special. As long as a property (here:
of the function context's Variable Object) stores a reference value, you can
use that property to change the referred object's properties.

> So, the right way to think about is the object "supplies the value to
> the function"?

No, `p' did not store the object in the first place; it only stored the
reference (value) to it. As does `q' below:

var p = {};

/*
* p
* ||
* ref ---> [object Object = {}]
*/

var q = p;

/*
* p
* ||
* ref ---> [object Object = {}]
* ||
* q
*/

q.a = 42;

/*
* p
* ||
* ref ---> [object Object = {a: 42}]
* ||
* q
*/

/* 42 */
p.a;

BTW, it's the same in Java and other OOPLs.

You want to fix your `From' header.


HTH

PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann