wrote:
> RobG написав:
> > scriptg...@gmail.com wrote:
> > > RobG написав:
> > > > Jeremy wrote:
> > > > [...]
> > > > > Javascript usually passes by value. When you start working with DOM
> > > > > nodes this gets a little fuzzy, but objects like strings, numbers, and
> > > > > anonymous objects are passed by value if I'm not mistaken.
> > > >
> > > > Maybe confused 
> > > >
> > > > In an assignment expression, the value of the right hand side is
> > > > assigned to the left hand side. If the value is a primitive, then that
> > > > is assigned. If its value is a reference to an object, then a
> > > > reference is assigned.
> > > >
> > > > e.g.
> > > >
> > > > var x = 2; // x is assigned the value of 2
> > > > var y = x; // y is assigned the value of x, which is 2
> > > >
> > > > var foo = {}; // foo is assigned a reference to an object
> > > > var bar = foo; // bar is assigned the value of foo,
> > > > // a reference to the same object
> > > > bar = y; // bar is now assigned the value of y, which is 2
> > > >
> > > > HTH
> > > >
> > > BTW it passes complex data type (objects/arrays) by reference and
> > > primitive data types (string/number/boolean etc.) by value. Check the
> > > JS specification.
> >
> > Is that intended to be a correction or a further explanation?
> It was a shorter explanation.
>
> var a=[1,2,3]
> function x(arr){
> arr.pop()
> }
> x(a)
>
> Var passed by reference.
Sorry, but I find such explanations confusing and unhelpful - I'd
rather use the terms and expressions contained in the language
specification. If someone doesn't understand how an assignment
operator works, the above example won't help them at all.
In your use of:
var a = [1,2,3];
you should explain that "a" is assigned a reference to the Array object
created on the right. You might then have said that:
var b = a;
will result in b being assigned the same value as a, so that now b is a
reference to the same array. So the value of a variable is always
"passed by value". Saying that some are "passed by reference" and that
others are "passed by value" leads to a fundamental misunderstanding of
how assignment works and of what the value of a variable is.
I don't know where the term "passed" came from originally, but the use
of terminology from some other context assumes that the person
implicitly understands the same (unstated) context. They may well
spend more time trying to learn and apply the foreign terminology to an
inappropriate situation than in understanding the answer the original
question.
Recent discussions on JavaScript Objects and hash tables spring to
mind.
--
Rob