Steven D'Aprano
5/22/2015 7:20:00 PM
On Sat, 23 May 2015 03:14 am, Stefan Ram wrote:
> Supersedes: <pass-20150522184523@ram.dialup.fu-berlin.de>
>
> Steven D'Aprano <steve@pearwood.info> writes:
>>Javascript has two kinds of values, simple values or primitives, and
>>objects. There are three primitive types: number, string and boolean.
>
> string
> number
> boolean
> null
> undefined
> symbol (new in ECMAScript 6)
Thanks for the correction.
>>I understand that for primitives, if I pass a value to a function,
>>Javascript uses pass by value: the value is copied. (This is the same
>>behaviour as, say, C or Pascal.) While accepting that on faith is fine,
>>I'd like to actually demonstrate it with some Javascript code.
>>In C or Pascal, I could use the "address of" operator to compare the value
>>inside the function with the value outside the function, to see if they
>>referred to the same memory location. In Javascript, I could use something
>>like this:
>
> A value does not have an address.
Numbers in Javascript are IEEE 754 64-bit floating point quantities, so if I
do "var x = 23;" the 64-bit float with value 23.0 must be somewhere.
Perhaps you mean that the address is unavailable to Javascript code?
> Expressions for such
> values are known as »prvalues« in C++. With (pr)values,
> it makes no sense to make this distinction between »pass by
> value" and "pass by reference".
I know very little about C++, but speaking more generally, pass by value and
pass by reference have VERY different semantics and it is nonsense to say
that there is no distinction between them. Pass by reference has strict
requirements: the argument passed must be an actual variable, not an
arbitrary expression or constant (early Fortran compilers were notorious
for a bad bug where you could literally change the value of numeric
constants by passing then by reference), and the function parameter is
treated as an alias to that variable for assignment. Pass by value is very
different.
> One might also say it this
> way: "The value of a prvalue doubles as it's reference."
When I assign a value to a variable:
var x = 42;
the value of x that I am talking about is 42, not some reference to 42.
>> Is there anything I can write in
>>Javascript to demonstrate that when I pass a primitive value into a
>>function, it is copied, but when I pass an object into the function, it is
>>not?
>
> In the case of a value, you show it by showing that it just works.
What does "it just works" mean? What's "it"? That I can pass an argument to
a function?
Unfortunately, "I can pass arguments to functions" works equally well
whether the calling convention is by value, by name, by reference, by need,
by sharing, or something else. The only calling convention that can be
ruled out is call by reference, because I can pass expressions as arguments
(pass by reference parameters must be lvalues).
> You can show that objects are not copied in this case
> by showing alias effects.
>
> function modify( object ){ object.field = 5; }
> function main()
> { var object = {}; modify( object ); console.log( object.field ); }
> main();
Yes, I'm aware of that. But I'm asking specifically about primitive types,
not objects. Using rhino:
js> function modify( object ){
> object.field = 5;
> print(object.field);
> }
js> x = 23;
23
js> modify(x);
undefined
So your test doesn't work here.
--
Steven