From: Thomas 'PointedEars' Lahn on
Johannes Baagoe wrote:

> Thomas 'PointedEars' Lahn :
>> We've been over this a thousand times or so, so I try to keep my answers
>> short and concise
>
> If it has been discussed a thousand times or so, shouldn't it be
> considered a FAQ ?

Maybe it should. Apparently nobody has come up with a summarizing text yet.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
From: Jorge on
On Oct 21, 7:52 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> Johannes Baagoe wrote:
>
> > According to the tutorial at
> >https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Functions:
>
> >   Object parameters, e.g. objects, arrays and regular expressions,
> >   are passed to functions by reference
>
> That is wrong.  Someone competent should use the Wiki functionality of MDC
> and correct it ASAP.

That's not wrong it's perfectly right so don't touch it unless you
want to look like a fool or even worse.

http://en.wikipedia.org/wiki/Reference_(computer_science)
--
Jorge.
From: John G Harris on
On Wed, 21 Oct 2009 at 19:52:45, in comp.lang.javascript, Thomas
'PointedEars' Lahn wrote:

<snip>
>It is actually very simple to understand if you accept the specified truth
>that object references are values (of the internal Reference type).
<snip>

Unfortunately, ECMA 262 defines members of the Reference type to be
something different from what most people would call a reference.

A Reference selects a particular property of a particular object.

A reference is something that behaves like a pointer to an object.
(There's more than one way they could be implemented).

John
--
John Harris
From: Thomas 'PointedEars' Lahn on
Jorge wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Johannes Baagoe wrote:
>> > According to the tutorial at
>>
>https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Functions:
>>
>> > Object parameters, e.g. objects, arrays and regular expressions,
>> > are passed to functions by reference
>>
>> That is wrong. Someone competent should use the Wiki functionality of
>> MDC and correct it ASAP.
>
> That's not wrong it's perfectly right

No, it is not right; apparently you have not been paying attention to what I
said. (My posting consisted of much more than what you quoted.)

"Pass by reference" has a specific meaning in programming that does not
apply here. "Pass by reference" means that a pointer to a memory location
is passed so that the data at this memory location can be directly modified,
e.g. overwritten with new data, when using this pointer.

That is not the case with ECMAScript objects as Johannes' test cases have
clearly showed, too: Assigning a value to `a' does _not_ cause the object to
be redefined. The following makes it clear why not (we've been over this
ad nauseam already, but since you insist):

var bar = ['a']; bar ---> ['a'] <--.
^ |
function foo(a) a -------' |
{ |
a = [1]; a -----> [1] |
} |
|
foo(bar); bar --------------'

IOW: After `a' has been assigned `[1]', it refers to a different object
than before. Different not only in value, but in identity; as the result of

bar[0]

clearly shows, the previously referred object continues to exist (as long as
there is a reference to it; take that away and it becomes inaccessible and
is going to be marked for garbage collection). The memory allocated for it
has _not_ been overwritten through the assignment in the function.

> so don't touch it unless you want to look like a fool or even worse.

So more like you?

> http://en.wikipedia.org/wiki/Reference_(computer_science)

You should read it and then consider the repercussions if this would apply
to ECMAScript implementations, should all the examples given have not
managed to convince you that it does not apply here.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
From: VK on
Johannes Baagoe wrote:
> why, exactly, do the following functions yield the shown results ?
>
>   function foo(a) {a = [1];} foo(bar);          // bar[0] === 'a'
>   function foo(a) {a[0] = 1;} foo(bar);         // bar[0] === 1
>   function foo(a) {a = []; a[0] = 1;} foo(bar); // bar[0] === 'a'
>   function foo(a) {a[0] = 1; a = [];} foo(bar); // bar[0] === 1

This test set is somewhat misleading because it indirectly implies
that something particularity special happens with objects while with
primitives it's some other story. These two cases in JavaScript are
totally equal by their internal mechanics:

var num1 = 1;
var num2 = num1;

var arr1 = [1,2,3]
var arr2 = arr1;

window.alert(''+num1+' '+num2); // 1 1
f(num2);
window.alert(''+num1+' '+num2); // 1 1

window.alert(''+arr1+' '+arr2); // 1,2,3 1,2,3
f(arr2);
window.alert(''+arr1+' '+arr2); // 1,2,3 1,2,3

function f(arg) {
arg = 'foobar';
}

The truth is that in JavaScript everything from 0 to a complex object
is a reference, but a reference of a special kind: they cannot be used
to access the value, because they don't point to it. They only point
to the scavenger with one of relevant fields in it referencing the
value. The other thing in JavaScript is that references obtained from
the reference table (dispid's in JScript terms) are not reusable and
being retrieved over and over again - lesser some internal code
optimization - on each call. All this is the price to pay for loosely
typed structure.

So in this example:
var bar = ['a'];
function foo(a) {a = [1];} foo(bar); // bar[0] === 'a'

1)
[scavenger] <- JS_reference <- name table item "bar"
|_ ["a"]

2)
[scavenger] <- JS_reference <- name table item "bar"
|_ ["a"] <- name table item "a"

3)
[scavenger] <- JS_reference <- name table item "bar"
|_["a"]

[scavenger] <- JS_reference <- name table item "a"
|_[1]
4)
exit from function, name table item "a" removed

5)
[scavenger] <- JS_reference <- undefined
|_[1]

6)
Referencer pass, check name table, no relevant records, mark GCA
(Garbage Collection Allowed)

Garbage collector pass, check GCA flags, remove JS_reverence and
scavenger itself. btw depending on the system load and some system
level settings from the step 4) to step 6) the maximum delay is up to
180sec (3min) and rarely lesser than 60sec unless the system is on a
crucial memory shortage so GC runs more often - or unless someone is
slowing down everything by using left and right undocumented
CollectGarbage() method.
Such delay is totally OK for memory management free environment but I
know that it makes many C-grounded developers highly unhappy and
nervious.

You may find interesting my explanations and other's critics in the
"Questions about memory structure of value types and reference types"
discussion from 2006:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/9c39023d7d9d96d2/38ed90f1c1b4ccef