Revision 164578 of Defining Functions

  • Revision slug: Talk:JavaScript/Guide/Obsolete_Pages/Defining_Functions
  • Revision title: Defining Functions
  • Revision id: 164578
  • Created:
  • Creator: PauloAugusto
  • Is current revision? No
  • Comment

Revision Content

PauloAugusto: I think the prose about by-value parameters was correct before your rewrite. It's true that you can change the passed object's properties, but the "object reference" is itself passed by value:

> function x(o) { o = {a:2} }
> var obj = {a:1}
> x(obj)
> print(obj.a)
1

-- Nickolay 02:03, 15 September 2007 (PDT)


"All parameters are passed by value ..." is wrong and it misleaded me the first time i read it. Only primitives are passed by value, all others are passed by reference.

Also, the description of what it means to pass "by value" is poor. At the moment it is like saying that if you turn on switch x, light bulb y turns on. The same goes for the explanation of how "by reference" works. There are no special rules that say that, when passed "by reference", changes are seen outside the calling function. The truth is that the reference to the object is passed to the function and, when the function accesses that variable, it is, in fact, accessing the same object. Nothing magical happens based on some arbitrary rules.

Also of note, you mention that "the "object reference" is itself passed by value". As far as i know, that is the exact definition of passing "by reference".
--PauloAugusto 17-9-2007, the one who knows nothing about wikies, bhá!

Well, I couldn't find anything authoritative on this issue, but my feeling (based on C++ background) is that this is not called 'passing by reference'. Here's an article by someone else who thinks like me: (the article is about java, but the idea is the same -- see the swap test). --Nickolay 20:46, 17 September 2007 (PDT)


In "C" terms:

> Function (aVariable); //This is Pass by Value. Function will receive a copy of "aVariable".
> Function (&aVariable); //This is Pass by Reference. Function will receive a memory address, a 'value' indicating where that variable is stored, the "reference".

In Javascript, a number is passed by value (or by copy, if you prefer). A object is automatically passed by reference, without the need to specifically state it with the char '&' like in C/C++.
Objects are *not* passed to functions by value, unlike it is stated in the explanation and like that very same example shows... The explanation is wrong!
English is a very undefined language, unlike most programing languages (often based on it). I hate it when there is the need to write stuff that need to be very clear.
-- Paulo Augusto, 18-9-2007

Revision Source

<p>PauloAugusto: I think the prose about by-value parameters was correct before your rewrite. It's true that you can change the passed object's properties, but the "object reference" is itself passed by value:
</p>
<pre class="eval">&gt; function x(o) { o = {a:2} }
&gt; var obj = {a:1}
&gt; x(obj)
&gt; print(obj.a)
1
</pre>
<p>-- <a href="User:Nickolay">Nickolay</a> 02:03, 15 September 2007 (PDT)
</p><p><br>
"All parameters are passed by value ..." is wrong and it misleaded me the first time i read it. Only primitives are passed by value, all others are passed by reference.
</p><p>Also, the description of what it means to pass "by value" is poor. At the moment it is like saying that if you turn on switch x, light bulb y turns on. The same goes for the explanation of how "by reference" works. There are no special rules that say that, when passed "by reference", changes are seen outside the calling function. The truth is that the reference to the object is passed to the function and, when the function accesses that variable, it is, in fact, accessing the same object. Nothing magical happens based on some arbitrary rules.
</p><p>Also of note, you mention that "the "object reference" is itself passed by value". As far as i know, that is the exact definition of passing "by reference".<br>
--PauloAugusto 17-9-2007, the one who knows nothing about wikies, bhá!
</p>
<dl><dd> Well, I couldn't find anything authoritative on this issue, but my feeling (based on C++ background) is that this is not called 'passing <i>by</i> reference'. Here's an article by someone else who thinks like me: <a class="external" href="http://javadude.com/articles/passbyvalue.htm"> (the article is about java, but the idea is the same -- see the <code>swap</code> test). --</a><a href="User:Nickolay">Nickolay</a> 20:46, 17 September 2007 (PDT)
</dd></dl>
<p><br>
In "C" terms:
</p>
<pre class="eval">&gt; Function (aVariable); //This is Pass by Value. Function will receive a copy of "aVariable".
&gt; Function (&amp;aVariable); //This is Pass by Reference. Function will receive a memory address, a 'value' indicating where that variable is stored, the "reference".
</pre>
<p>In Javascript, a number is passed by value (or by copy, if you prefer). A object is automatically passed by reference, without the need to specifically state it with the char '&amp;' like in C/C++.<br>
Objects are *not* passed to functions by value, unlike it is stated in the explanation and like that very same example shows... The explanation is wrong!<br>
English is a very undefined language, unlike most programing languages (often based on it). I hate it when there is the need to write stuff that need to be very clear.<br>
-- Paulo Augusto, 18-9-2007
</p>
Revert to this revision