mozilla

Revision 164576 of Defining Functions

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

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)

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>
Revert to this revision