L
Lasse Reichstein Nielsen
Evertjan. said:Lasse Reichstein Nielsen wrote on 26 jun 2011 in comp.lang.javascript:
Evertjan. said:Stanimir Stamenkov wrote on 26 jun 2011 in comp.lang.javascript:
26 Jun 2011 08:10:04 GMT, /Evertjan./:
The scope difference of passing by reference or by value when used
diligently, can save far more time and code lines than the simplistic
implementation referred to here.
As far as I'm aware, in JS one always passes values - either
references to objects, or primitive values. With pass-by-reference
I understand:
No, your understanding is wrong.
No, it is correct.
Variables are passed by value,
[meaning that only the value is passed]
objects are passed by reference,
[meaning thet the object-pointer is passed.]
That's not what "pass by reference" means (in the sense used in
computer science, which was where the terminology was invented). In
pass-py-reference, you pass a reference to a *variable*, not to a
*value*.
As I showed above. You are not adding anything.
No. What you showed was *not* passing a variable. It was passing
a reference to a shared mutable data structure (an object).
As I showed above. You are not adding anything.
You are missing the point.
That you ALSO can call it by another name is fine.
That's not a different name for the same thing. It's a different
name for a DIFFERENT thing.
I think your argumentation is therefore utter nonsense.
I will expand my above reasoning:
Function arguments containing a variable
How can an argument "contain" a variable?
Inside the function, the argument is available *as* a variable
(the formal parameter of the function).
Outside, it's just a value.
only pass IT'S CONTAINING VALUE
What is a "containing value"? Which value is it and what does it
contain?
and NOT THE REFEREBENCE TO THE VARIABLE.
Are you saying that calling the function with a variable expression as
an argument, the value passed as the argument to the call is the value
bound to the variable, and not a reference to the variable itself?
In that case, yeS:
So the argument name is a new variable with local scope.
This is called "passing by value".
This is exactly passing by value.
While function arguments containing an object
pass the REFERENCE TO THAT OBJECT,
and NOT IT'S CONTENTS.
If you write
foo(o)
and o is a variable bound to an object, then o really holds
a reference to the object, not the object itself.
That's why you can make assignments without breaking identity:
var o1 = {x:42};
var o2 = o1;
o2.x = 37;
alert(o1.x); // 37.
So the argument name is just another name of the object
where only the name has local scope.
No, argument name (aka. formal parameter name) defines a local
variable holding the same value as the one passed in.
So if you have:
function foo(x) { x.y = 42; x = 10;}
var o = {y:37};
foo(o);
alert(o.y);
then you pass the value of o, the reference to the object, into
foo. Then foo's x and the surrounding scope's o variables have the same
*value*. However, they are not the *same variable*. That is why doing
x = 10;
does not make o have the value 10.
If it had been pass-by-reference, then o would have had the value 10.
This is called "passing by reference".
No, it's not. Passing by reference makes the argument variable and
*alias* of the passed in variable. Changing one will change the other.
That does not happen here.
It's pass by sharing, which is different from pass by reference, as
explained in, e.g., the wikipedia article.
Javascript/ECMAScript only have pass-by-sharing. It doesn't have
pass-by reference.
C++ has pass-by-reference. Example:
#include <iostream>
using namespace std;
class A {
public:
int x;
}
void by_reference(A& x) { x.x = 10; }
void by_value(A x) { x.x = 10; }
int main(int argc, char* argv[]) {
A o = { 42 }; // o is a variable holding an object.
cout << o.x << endl; // prints 42
by_value(o); // Passing the value of o by value.
cout << o.x << endl; // prints 42
by_reference(o); // Passing o by reference.
cout << o.x << endl; // prints 10
return 0;
}
You can't do that in Javascript.
/L