Why does the assignment operator return a value and not a reference?
Posted
by Nick Lowman
on Stack Overflow
See other posts from Stack Overflow
or by Nick Lowman
Published on 2010-06-15T17:34:49Z
Indexed on
2010/06/15
17:42 UTC
Read the original article
Hit count: 166
JavaScript
I saw the example below explained on this site and thought both answers would be 20 and not the 10 that is returned. He wrote that both the comma and assignment returns a value, not a reference. I don't quite understand what that means.
I understand it in relation to passing variables into functions or methods i.e primitive types are passed in by value and objects by reference but I'm not sure how it applies in this case.
I also understand about context and the value of 'this' (after help from stackoverflow) but I thought in both cases I would still be invoking it as a method, foo.bar() which would mean foo is the context but it seems both result in a function call bar().
Why is that and what does it all mean?
var x = 10;
var foo = {
x: 20,
bar: function () {return this.x;}
};
(foo.bar = foo.bar)();//returns 10
(foo.bar, foo.bar)();//returns 10
© Stack Overflow or respective owner