Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #13007 > unrolled thread
| Started by | Andreas Bergmaier <andber93@web.de> |
|---|---|
| First post | 2012-03-23 13:56 +0100 |
| Last post | 2012-03-24 11:40 +0000 |
| Articles | 16 on this page of 36 — 14 participants |
Back to article view | Back to comp.lang.javascript
call by reference Andreas Bergmaier <andber93@web.de> - 2012-03-23 13:56 +0100
Re: call by reference Scott Sauyet <scott.sauyet@gmail.com> - 2012-03-23 07:10 -0700
Re: call by reference "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-23 15:23 +0000
Re: call by reference Scott Sauyet <scott.sauyet@gmail.com> - 2012-03-23 09:28 -0700
Re: call by reference "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-23 19:41 +0000
Re: call by reference Gene Wirchenko <genew@ocis.net> - 2012-03-23 13:43 -0700
Re: call by reference "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-23 21:18 +0000
Re: call by reference BGB <cr88192@hotmail.com> - 2012-03-23 15:23 -0700
Re: call by reference Gene Wirchenko <genew@ocis.net> - 2012-03-23 15:27 -0700
Re: call by reference "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-24 00:19 +0000
Re: call by reference Norman Peelman <npeelmandog@cfl.rr.com> - 2012-03-28 06:43 -0400
Re: call by reference "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-28 14:50 +0000
Re: call by reference Aatu Koskensilta <aatu.koskensilta@uta.fi> - 2012-03-28 18:06 +0300
Re: call by reference "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-28 15:57 +0000
Re: call by reference Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-03-28 23:22 +0200
Re: call by reference "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-29 07:18 +0000
Re: call by reference Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-03-29 13:34 +0200
Re: call by reference David Mark <dmark.cinsoft@gmail.com> - 2012-03-29 18:30 -0700
Re: call by reference Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-04-03 00:09 +0200
Re: call by reference David Mark <dmark.cinsoft@gmail.com> - 2012-04-02 17:43 -0700
Re: call by reference Robert Baer <robertbaer@localnet.com> - 2012-03-30 00:49 -0800
Re: call by reference Scott Sauyet <scott.sauyet@gmail.com> - 2012-03-28 13:33 -0700
Re: call by reference John G Harris <john@nospam.demon.co.uk> - 2012-03-24 11:34 +0000
Re: call by reference Aatu Koskensilta <aatu.koskensilta@uta.fi> - 2012-03-26 22:46 +0300
Re: call by reference Gene Wirchenko <genew@ocis.net> - 2012-03-26 18:38 -0700
Re: call by reference Aatu Koskensilta <aatu.koskensilta@uta.fi> - 2012-03-27 05:13 +0300
Re: call by reference Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-27 18:31 +0200
Re: call by reference Aatu Koskensilta <aatu.koskensilta@uta.fi> - 2012-03-27 21:31 +0300
Re: call by reference Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-27 21:03 +0200
Re: call by reference "Ross A. Finlayson" <ross.finlayson@gmail.com> - 2012-04-10 22:03 -0700
Re: call by reference Scott Sauyet <scott.sauyet@gmail.com> - 2012-03-25 07:34 -0700
Re: call by reference "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-25 15:53 +0000
Re: call by reference Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-03-27 05:15 -0700
Re: call by reference BGB <cr88192@hotmail.com> - 2012-03-23 13:03 -0700
Re: call by reference Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-23 23:02 +0100
Re: call by reference John G Harris <john@nospam.demon.co.uk> - 2012-03-24 11:40 +0000
Page 2 of 2 — ← Prev page 1 [2]
| From | Robert Baer <robertbaer@localnet.com> |
|---|---|
| Date | 2012-03-30 00:49 -0800 |
| Message-ID | <BvadnaQ_TMgB-OjSnZ2dnUVZ_hOdnZ2d@posted.localnet> |
| In reply to | #13109 |
Evertjan. wrote: > Eric Bednarz wrote on 28 mrt 2012 in comp.lang.javascript: > >> "Evertjan."<exjxw.hannivoort@interxnl.net> writes: >> >> [...] >> >>> so one should not use such words in this NG, >> >> Yep. > > Quite. > > What about "Yep", Eric? > > Well, for one, use of "yep" too many times would make you a "yeppie" which should not be confused with a "yuppie" because of the different vowel..
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-03-28 13:33 -0700 |
| Message-ID | <b3061762-f238-4458-aed2-781b2fb4c04b@d17g2000vba.googlegroups.com> |
| In reply to | #13104 |
Evertjan wrote: > The point is it is local slang, with the stress on local, > so one should not use such words in this NG, > it is slang nevertheless. Nope.
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2012-03-24 11:34 +0000 |
| Message-ID | <pGQAh1E+EbbPFwrt@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #13019 |
On Fri, 23 Mar 2012 at 15:27:43, in comp.lang.javascript, Gene Wirchenko wrote: <snip> > Actual parameters are the parameters in a call. The parameters >as declared in the function are called formal parameters. <snip> In a maths book 'parameter' is what you see in the function definition and 'argument' is what you supply when you call the function. Computer science sometimes uses this terminology and sometimes uses formal/actual :-( John -- John Harris
[toc] | [prev] | [next] | [standalone]
| From | Aatu Koskensilta <aatu.koskensilta@uta.fi> |
|---|---|
| Date | 2012-03-26 22:46 +0300 |
| Message-ID | <87ehsfcfie.fsf@uta.fi> |
| In reply to | #13015 |
"Evertjan." <exjxw.hannivoort@interxnl.net> writes: > Gene Wirchenko wrote on 23 mrt 2012 in comp.lang.javascript: > >> Nope. > > "Nope"? > Could you speak normal English in this international NG, please. "Nope" is perfectly normal English. > Please explain what we should call the passing of an object in > javascript. In general it's not possible to pass an object as an argument for a function in Javascript, by reference or otherwise. What is possible is to pass by value a reference to an object. -- Aatu Koskensilta (aatu.koskensilta@uta.fi) "Wovon man nicht sprechen kann, darüber muss man schweigen." - Ludwig Wittgenstein, Tractatus Logico-Philosophicus
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2012-03-26 18:38 -0700 |
| Message-ID | <b362n75j9tacrogtdhem5ldo1a1i2jatc3@4ax.com> |
| In reply to | #13070 |
On Mon, 26 Mar 2012 22:46:01 +0300, Aatu Koskensilta
<aatu.koskensilta@uta.fi> wrote:
>"Evertjan." <exjxw.hannivoort@interxnl.net> writes:
>
>> Gene Wirchenko wrote on 23 mrt 2012 in comp.lang.javascript:
>>
>>> Nope.
>>
>> "Nope"?
>> Could you speak normal English in this international NG, please.
>
> "Nope" is perfectly normal English.
>
>> Please explain what we should call the passing of an object in
>> javascript.
>
> In general it's not possible to pass an object as an argument for a
>function in Javascript, by reference or otherwise. What is possible is
>to pass by value a reference to an object.
"In general"? How is it *ever* possible to pass an object by
reference? I can not think of a way. If there is one, I would really
like to know.
The only way that I can think of that would come close would be
embedding the object references as an object properties. Something
like:
Obj1={Word1: "Hello", Word2: "World"};
Obj2={Word1: "Good", Word2: "night"};
Obj3={Ref1: Obj1, Ref2: Obj2};
function Swap(theObject)
{
var ObjTemp=theObject.Ref1;
theObject.Ref1=theObject.Ref2;
theObject.Ref2=ObjTemp;
}
Swap(Obj3);
which -- if I have not goofed somewhere -- should swap the values of
the Obj1 and Obj2 properties.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Aatu Koskensilta <aatu.koskensilta@uta.fi> |
|---|---|
| Date | 2012-03-27 05:13 +0300 |
| Message-ID | <8762dqdc4d.fsf@uta.fi> |
| In reply to | #13076 |
Gene Wirchenko <genew@ocis.net> writes: > On Mon, 26 Mar 2012 22:46:01 +0300, Aatu Koskensilta > <aatu.koskensilta@uta.fi> wrote: > >> In general it's not possible to pass an object as an argument for a >>function in Javascript, by reference or otherwise. What is possible is >>to pass by value a reference to an object. > > "In general"? How is it *ever* possible to pass an object by > reference? I can not think of a way. If there is one, I would really > like to know. As mentionted in this thread, apparently the arguments object is passed by reference. -- Aatu Koskensilta (aatu.koskensilta@uta.fi) "Wovon man nicht sprechen kann, darüber muss man schweigen." - Ludwig Wittgenstein, Tractatus Logico-Philosophicus
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-03-27 18:31 +0200 |
| Message-ID | <2491914.vAMVHVRlcs@PointedEars.de> |
| In reply to | #13077 |
Aatu Koskensilta wrote: > Gene Wirchenko <genew@ocis.net> writes: >> Aatu Koskensilta wrote: >>> In general it's not possible to pass an object as an argument for a >>> function in Javascript, by reference or otherwise. What is possible is >>> to pass by value a reference to an object. >> >> "In general"? How is it *ever* possible to pass an object by >> reference? I can not think of a way. If there is one, I would really >> like to know. > > As mentionted in this thread, apparently the arguments object is > passed by reference. As explained in this thread, it is not. 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
[toc] | [prev] | [next] | [standalone]
| From | Aatu Koskensilta <aatu.koskensilta@uta.fi> |
|---|---|
| Date | 2012-03-27 21:31 +0300 |
| Message-ID | <871uoddhf7.fsf@uta.fi> |
| In reply to | #13089 |
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes: > Aatu Koskensilta wrote: > >> As mentionted in this thread, apparently the arguments object is >> passed by reference. > > As explained in this thread, it is not. Right. We could say the arguments object allows one to simulate pass by reference for certain variables in certain special situations, but this is pedantry more tedious than even I have much inclination for. -- Aatu Koskensilta (aatu.koskensilta@uta.fi) "Wovon man nicht sprechen kann, darüber muss man schweigen." - Ludwig Wittgenstein, Tractatus Logico-Philosophicus
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-03-27 21:03 +0200 |
| Message-ID | <1611034.kdTlGXAFRq@PointedEars.de> |
| In reply to | #13094 |
Aatu Koskensilta wrote:
> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Aatu Koskensilta wrote:
>>> As mentionted in this thread, apparently the arguments object is
>>> passed by reference.
>>
>> As explained in this thread, it is not.
>
> Right. We could say the arguments object allows one to simulate pass
> by reference for certain variables in certain special situations, but
> this is pedantry more tedious than even I have much inclination for.
You are missing the point.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
[toc] | [prev] | [next] | [standalone]
| From | "Ross A. Finlayson" <ross.finlayson@gmail.com> |
|---|---|
| Date | 2012-04-10 22:03 -0700 |
| Message-ID | <1f6560f7-7737-4e16-9d92-a4a371cef7ec@z3g2000pbn.googlegroups.com> |
| In reply to | #13094 |
On Mar 27, 11:31 am, Aatu Koskensilta <aatu.koskensi...@uta.fi> wrote: > Thomas 'PointedEars' Lahn <PointedE...@web.de> writes: > > > Aatu Koskensilta wrote: > > >> As mentionted in this thread, apparently the arguments object is > >> passed by reference. > > > As explained in this thread, it is not. > > Right. We could say the arguments object allows one to simulate pass > by reference for certain variables in certain special situations, but > this is pedantry more tedious than even I have much inclination for. > > -- > Aatu Koskensilta (aatu.koskensi...@uta.fi) > > "Wovon man nicht sprechen kann, darüber muss man schweigen." > - Ludwig Wittgenstein, Tractatus Logico-Philosophicus > > "Coding"
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-03-25 07:34 -0700 |
| Message-ID | <c8571c1b-d765-40f4-a002-07fa330a6bda@l14g2000vbe.googlegroups.com> |
| In reply to | #13012 |
Evertjan wrote:
> Scott Sauyet wrote:
>> "Evertjan." wrote:
>>> Andreas Bergmaier wrote:
>>>> I know, JavaScript does not support call-by-reference. Every function
>>>> invocation does call-by-value, even if that value is a reference value
>>>> to an array-/function-/plain-Object, and not a primitive.
>
>>> JavaScript DOES support call-by-reference.
>
>>> All objects are called by reference:
>
>> No, references are passed by value. It's perhaps a subtle
>> distinction, but it's quite important. [ ... ]
>
> Reference in my book, in Javascript or any other language,
> is transferring the variable-pointer or object-pointer and not the value.
It's not clear to me if you've understood the distinction being made
here. The usual definition of "pass by reference" would yield the
behavior described by BGB:
function increment(x) {x++;}
var x=3;
increment(x);
alert(x); // 4 if x was passed by reference, 3 if by value
Yes, when we pass objects to functions, we pass something like
pointers to these objects, but the pointers inside the function are
*copies* of the pointers supplied when calling the function, not the
original pointers. This is easy to see by reassigning the parameters
inside the function to new objects and noting that the pointers of the
original caller are not changed.
A pretty good description of the difference is a C# article by John
Skeet [1] Even if you don't know the language syntax, I think it's
reasonably easy to understand.
-- Scott
[1] http://www.yoda.arachsys.com/csharp/parameters.html
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exjxw.hannivoort@interxnl.net> |
|---|---|
| Date | 2012-03-25 15:53 +0000 |
| Message-ID | <XnsA021B5FE99F70eejj99@194.109.133.133> |
| In reply to | #13055 |
Scott Sauyet wrote on 25 mrt 2012 in comp.lang.javascript: > Yes, when we pass objects to functions, we pass something like > pointers to these objects, but the pointers inside the function are > *copies* of the pointers supplied when calling the function, not the > original pointers. This is easy to see by reassigning the parameters > inside the function to new objects and noting that the pointers of the > original caller are not changed. Good point. Logical reasoning can easily change my 'book', where "widely understood in other programming languages" does not. It boils down to what you mean by "reference", a pointer being a "reference" in optima forma. However I can live with your reasoning. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Asen Bozhilov <asen.bozhilov@gmail.com> |
|---|---|
| Date | 2012-03-27 05:15 -0700 |
| Message-ID | <16809571.1415.1332850535967.JavaMail.geo-discussion-forums@vbex14> |
| In reply to | #13009 |
Evertjan:
> JavaScript DOES support call-by-reference.
>
> All objects are called by reference:
This is long discussed topic. Your terminology even and it is not correct seems more closer to the point. JavaScript uses the evaluation strategy called "Call by sharing".
<URL: http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing>
It is often mistaken with "Call by reference", but it slightly differ:
"The semantics of call-by-sharing differ from call-by-reference in that assignments to function arguments within the function aren't visible to the caller (unlike by-reference semantics)[citation needed], so e.g. if a variable was passed, it is not possible to simulate an assignment on that variable in the caller's scope. However since the function has access to the same object as the caller (no copy is made), mutations to those objects, if the objects are mutable, within the function are visible to the caller, which may appear to differ from call-by-value semantics."
> <script type='text/javascript'>
>
> var myObj = {};
>
> myObj.myValue = 7;
> alert(myObj.myValue); // 7
>
> function f(a) {
> a.myValue = 33;
> };
>
> f(myObj);
> alert(myObj.myValue); // 33
>
> </script>
It does not prove your point.
function f(o) {
o.foo = 10;
o = {foo : 0};
}
var obj = {foo : 5};
f(obj);
print(obj.foo); //10
This example shows that JavaScript does not use "Call by reference" if it used the value of `foo` would be 0 instead. The example shows "Call by sharing" strategy.
[toc] | [prev] | [next] | [standalone]
| From | BGB <cr88192@hotmail.com> |
|---|---|
| Date | 2012-03-23 13:03 -0700 |
| Message-ID | <jkil11$8m4$1@news.albasani.net> |
| In reply to | #13007 |
On 3/23/2012 5:56 AM, Andreas Bergmaier wrote:
> I know, JavaScript does not support call-by-reference. Every function
> invocation does call-by-value, even if that value is a reference value
> to an array-/function-/plain-Object, and not a primitive.
>
> Yet, there is a javascript thing that stores values by reference: the
> arguments object.
>
> > function kill(a) { for (var i=0;i<a.length;i++) a[i]=false; }
> > function x(a,b,c) { kill(arguments); return[a,b,c] };
> > x(true,true,true)
> [false, false, false]
>
> I did not assign to a, b or c. Can this be termed "call-with-references"
> or something?
>
one option: "call by value" with "reference types".
although not proper JS, my own ES-variant (used mostly for game
scripting) partly adds references as a language extension:
foo(&x, &y);
which may be handled at the target either as:
function foo(rx, ry)
{ *rx=3; *ry=4; }
or, as:
function foo(&x, &y)
{ x=3; y=4; }
which is basically syntax sugar for the above.
functionally "*rx=3;" is more-or-less equivalent to "rx[0]=3;".
note: although vaguely resembling them, these are not to be confused
with C-style pointers (functionally, the reference is "boxed", and
deferred assignment is made into this box).
as-is, the "&x" notation at the call-site is mandatory, given that the
compiler doesn't necessarily know the signature of the call target
(otherwise, things wont work correctly).
or such...
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-03-23 23:02 +0100 |
| Message-ID | <5091390.GdagIssoVF@PointedEars.de> |
| In reply to | #13007 |
Andreas Bergmaier wrote:
> I know, JavaScript does not support call-by-reference. Every function
> invocation does call-by-value, even if that value is a reference value
> to an array-/function-/plain-Object, and not a primitive.
Correct.
> Yet, there is a javascript thing
There is no "javascript".
> that stores values by reference: the arguments object.
No. According to ECMAScript Edition 5(.1) [ES5], section 10.6, the
Arguments object has special internal [[Get]] and [[Set]] methods to allow
the observed behavior.
> > function kill(a) { for (var i=0;i<a.length;i++) a[i]=false; }
> > function x(a,b,c) { kill(arguments); return[a,b,c] };
> > x(true,true,true)
> [false, false, false]
>
> I did not assign to a, b or c. Can this be termed "call-with-references"
> or something?
Yes (it can be termed something).
The behavior you observed has been removed for strict code in [ES5].
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
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2012-03-24 11:40 +0000 |
| Message-ID | <LW7IVHGTKbbPFwvL@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #13017 |
On Fri, 23 Mar 2012 at 23:02:14, in comp.lang.javascript, Thomas 'PointedEars' Lahn wrote: <snip> >There is no "javascript". <snip> There is more than one "Thomas Lahn", yet people shouldn't be criticised for using the symbol. John -- John Harris
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.javascript
csiph-web