|
|||
|
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? Andreas Bergmaier |
|
|
||||
|
||||
|
|
|
|||
|
Andreas Bergmaier wrote:
> [ ... ] > 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? Sounds like a good name. Wow. I'd always insisted that JS has no form of call-by-reference. I'd never noticed that it actually does have this one. -- Scott |
|
|||
|
Andreas Bergmaier wrote on 23 mrt 2012 in comp.lang.javascript:
> 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: <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> > Yet, there is a javascript thing that stores values by reference: the > arguments object. This is just a special example of the general fact showen above. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) |
|
|||
|
"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. This means that in Javascript, unlike in some languages, you cannot do this: var f = function(out x) { // Error -- no output params x = "changed"; } var t = "original"; f(t); alert(t); // t == "changed"; The value passed as a parameter to the function is a *copy* of the reference to an object. There's no notion of an output parameter. Some languages have such facilities. Others do not. Until I saw the OP, I thought that Javsacript did not. But Andreas has shown that the `arguments` object provides an exception to this. The exception seems to be small. It only happens when you pass the `arguments` object to another function which treats it like an array and reassigns its elements. We couldn't modify the above code to work in JS without introducing another function and the only references that could be changed by another function are the ones used as parameters. But it's an exception nonetheless: var f = function(a) { a[0] = "changed"; } var g = function(t) { f(arguments); alert(t); // t is "changed"; } g("original"); Very interesting. -- Scott |
|
|||
|
Scott Sauyet wrote on 23 mrt 2012 in comp.lang.javascript:
> "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. This means that in Javascript, > unlike in some languages, you cannot do this: [...] Reference in my book, in Javascript or any other language, is transferring the variable-pointer or object-pointer and not the value. The fact is, that in Javascript objects are passed by their pointer. In other words, their pointer IS their value. So this clearly is passing by reference. That in othr languages you can choose does not mean that in Javascript objects are not passed by reference. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) |
|
|||
|
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... |
|
|||
|
On 23 Mar 2012 19:41:10 GMT, "Evertjan."
<exjxw.hannivoort@interxnl.net> wrote: >Scott Sauyet wrote on 23 mrt 2012 in comp.lang.javascript: > >> "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. This means that in Javascript, >> unlike in some languages, you cannot do this: > >[...] > >Reference in my book, in Javascript or any other language, >is transferring the variable-pointer or object-pointer and not the value. Nope. It is passing in such a way that the actual parameter can be changed (the one in the call). >The fact is, >that in Javascript objects are passed by their pointer. The question is whether that pointer value can be changed. If it can, it is call-by-reference. If not, it is call-by-value. Note that it is irrelevant whether a pointer value can be used to change another value as in passing an object reference and modifying one of the object's properties. >In other words, their pointer IS their value. >So this clearly is passing by reference. No. See the test page. If objects were passed by reference in JavaScript, then Fiddle()'s change of the value of the parameter would change the actual parameter, too. If you run it, you will see that it is not changed. >That in othr languages you can choose >does not mean that in Javascript objects are not passed by reference. The two are disconnected. ***** Start of Test Page ***** <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>2012-03-23: Object Parameters</title> </head> <body> <script type="text/javascript"> function Fiddle ( FiddleWithThis ) { var StartValue=FiddleWithThis; if (typeof(FiddleWithThis)=="object") FiddleWithThis=ThatObject; else FiddleWithThis="fiddled"; alert("FiddleWithThis changed: "+(FiddleWithThis!=StartValue)); } var SomeString="unchanged"; Fiddle(SomeString); alert("SomeString="+SomeString); var ThatObject=new Date(2001,1-1,1); var SomeObject=new Date(); Fiddle(SomeObject); alert("SomeObject="+SomeObject); </script> </body> </html> ***** End of Test Page ***** Sincerely, Gene Wirchenko |
|
|||
|
Gene Wirchenko wrote on 23 mrt 2012 in comp.lang.javascript:
>> Reference in my book, in Javascript or any other language, >> is transferring the variable-pointer or object-pointer >> and not the value. > > Nope. "Nope"? Could you speak normal English in this international NG, please. And why? Do you know my book? > It is passing in such a way that the actual parameter can > be changed (the one in the call). "actual"? Are there non-actual parameters in javascript? Please explain what we should call the passing of an object in javascript. == Is it by value? No, because you cannot change the object as such. == Is it by reference? Yes, because you can change the object's properties and methods, which are in fact the "values" of an object. So I maintain that objects are passed by reference. <script type='text/javascript'> var a = {x:9}; var b = {y:19}; function increment(p,z) { p[z]++; }; increment(a,'x'); increment(b,'y'); alert(a.x); // 10 alert(b.y); // 20 </script> -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) |
|
|||
|
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 |
|
|||
|
On 3/23/2012 2:18 PM, Evertjan. wrote:
> Gene Wirchenko wrote on 23 mrt 2012 in comp.lang.javascript: > >>> Reference in my book, in Javascript or any other language, >>> is transferring the variable-pointer or object-pointer >>> and not the value. >> >> Nope. > > "Nope"? > Could you speak normal English in this international NG, please. > > And why? > Do you know my book? > >> It is passing in such a way that the actual parameter can >> be changed (the one in the call). > > "actual"? Are there non-actual parameters in javascript? > > Please explain what we should call the passing of an object in > javascript. > > == Is it by value? > > No, because you cannot change the object as such. > if we define an object as a "reference type", then the "object reference" is being passed by-value. > == Is it by reference? > > Yes, because you can change the object's properties and methods, > which are in fact the "values" of an object. > this only goes so far as to say that one has a reference to the object's data (IOW, it is not being "physically" passed in the same manner as, say, a C struct). > So I maintain that objects are passed by reference. > "passing by reference" generally refers to the idea that it is the variable itself which is being passed by reference. when passing an object, one is passing a reference to the object, and not to the variable holding the object. these sorts of distinctions are important. > <script type='text/javascript'> > > var a = {x:9}; > var b = {y:19}; > > function increment(p,z) { > p[z]++; > }; > > increment(a,'x'); > > increment(b,'y'); > > alert(a.x); // 10 > alert(b.y); // 20 > > > </script> > this example does not show "pass by reference", it shows that one has "a reference to the object" (which is itself passed "by value"). what *would* show "pass by reference", would be if this were to work: function increment(x) { x++; } var x=3; increment(x); alert(x); // 4 given it does not (one will still see 3 here), one can conclude that it is not using "pass by reference". this is in-fact the default behavior in some languages (such as VB and Perl), but is relatively uncommon (most languages use "pass by value" instead, despite objects generally being "references"). http://en.wikipedia.org/wiki/Pass_by...l_by_reference note that many languages that do support pass-by-reference, do so via explicit syntax, for example: foo(&x); foo(ref x); foo(out x); .... |
|
|||
|
On 23 Mar 2012 21:18:37 GMT, "Evertjan."
<exjxw.hannivoort@interxnl.net> wrote: >Gene Wirchenko wrote on 23 mrt 2012 in comp.lang.javascript: > >>> Reference in my book, in Javascript or any other language, >>> is transferring the variable-pointer or object-pointer >>> and not the value. >> >> Nope. > >"Nope"? >Could you speak normal English in this international NG, please. No, I actually can not *speak* English on USENET as it is a written medium. (Your language snarking is not very nice.) >And why? >Do you know my book? No. >> It is passing in such a way that the actual parameter can >> be changed (the one in the call). > >"actual"? Are there non-actual parameters in javascript? No. Actual parameters are the parameters in a call. The parameters as declared in the function are called formal parameters. >Please explain what we should call the passing of an object in >javascript. > >== Is it by value? > >No, because you cannot change the object as such. Yes, because you can not change the formal parameter value and have it reflect on the actual parameter. >== Is it by reference? > >Yes, because you can change the object's properties and methods, >which are in fact the "values" of an object. No, because <same as above>. That that value is a pointer is irrelevant. >So I maintain that objects are passed by reference. Sorry, but you have it reversed. ><script type='text/javascript'> > >var a = {x:9}; >var b = {y:19}; > >function increment(p,z) { > p[z]++; >}; > >increment(a,'x'); > >increment(b,'y'); > >alert(a.x); // 10 >alert(b.y); // 20 > > ></script> The code does not change the value of a or b. That it changes the value of something that they point to is irrelevant. This might make my point better. Write a function that swaps two pointer values. Here is the driver code: var Obj1=new Date(2001,1-1,1); Var Obj2=new Date(); var Obj1Save=Obj1; var Obj2Save=Obj2; Swap(Obj1,Obj2); alert((Obj1==Obj2Save) && (Obj2==Obj1Save)); If you do it right, the alert should display true. Sincerely, Gene Wirchenko |
|
|||
|
Gene Wirchenko wrote on 23 mrt 2012 in comp.lang.javascript:
> (Your language snarking is not very nice.) Being very nice is not the topic of this NG. Please explain "snarking", is it part of ECMA? >>>> Nope Please explain "nope" > Sorry, but you have it reversed. Is that mainstream English? Why are you sorry? > Sincerely, Quite. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|