Hello and thanks for help
When I was writing some code I ran into a problem. In the example below. I was expecting the alert(a.x)
to output 1, instead it outputs 2. I have come to learn that it is because a
is being passed to this.b
as a reference. What I cannot seem to find, is how to pass it instead by value. (As in, I don't want to modify a
every time I call x()
)
var a = {"x":1}
function x() {
this.b = v;
this.b.x++;
}
x();
alert(a.x); //prints 2
I have also tried the following and other variants to no avail...
var a = {"x":1}
function x(v) {
this.b = v;
this.b.x++;
}
x(a);
alert(a.x); //... still prints 2
Can anyone clue me in on what I am missing?
Please and Thank You
(sidenote: this is a post that got to close to what I am talking about, but I couldn't figure out how to make it apply to my situation... if it is the same situation at all)