vince365
02-29-2012, 10:43 PM
Hi,
I am seeing an unexpected behaviour in the following code which I cannot explain however both FF and IE give the same result so the problem must be in my code.
I have reduced it to the following where I have 2 internal arrays a and b that are initialised to the same value T (which is an array).
If I make an update to one array I see the result in both a and b.
This only happens if I pass an array type. If I pass a single string (and re-code to remove [0] references) then the assignment to b does not update a.
What is the issue with using array's like this and why does the assignment to b cause a to take the same value?
The alerts display:
a Before : A,B
b Before : A,B
a After : X,B <<<<< WHY???
b After : X,B
<script language="JavaScript">
function test(T) {
this.a = T;
this.b = T;
// As expected both a and b display "A,B"
alert("a Before : "+this.a+"\nb Before : "+this.b);
// only update b
this.b[0]="X";
// why does a show the updated value of b?
alert("a After : "+this.a+"\nb After : "+this.b);
}
var tmp=new Array("A","B");
theTest=new test(tmp);
</script>
I am seeing an unexpected behaviour in the following code which I cannot explain however both FF and IE give the same result so the problem must be in my code.
I have reduced it to the following where I have 2 internal arrays a and b that are initialised to the same value T (which is an array).
If I make an update to one array I see the result in both a and b.
This only happens if I pass an array type. If I pass a single string (and re-code to remove [0] references) then the assignment to b does not update a.
What is the issue with using array's like this and why does the assignment to b cause a to take the same value?
The alerts display:
a Before : A,B
b Before : A,B
a After : X,B <<<<< WHY???
b After : X,B
<script language="JavaScript">
function test(T) {
this.a = T;
this.b = T;
// As expected both a and b display "A,B"
alert("a Before : "+this.a+"\nb Before : "+this.b);
// only update b
this.b[0]="X";
// why does a show the updated value of b?
alert("a After : "+this.a+"\nb After : "+this.b);
}
var tmp=new Array("A","B");
theTest=new test(tmp);
</script>