Update 2
I did another test in Chrome(the latest version), it doesn't have this issue. So I guess it's fixed in Chrome, but becomes an issue in Firefox. interesting :)
Update
I did my test in Firefox which just updated a few hours ago.(So it's not about the lazy evaluation in Chrome)
I was searching for whether JavaScript is a pass-by-reference or pass-by-value language, got the answer from here Is JavaScript a pass-by-reference or pass-by-value language?
Then I did a test which surprised me by its result:
var array = [1,2,3,4,5,6,7,8];
console.log(array);
change(array);
console.log(array);
function change(a)
{
var t = a[a.length -1] + 10;
var ta = a;
console.log('ta = '+ ta);
ta.push(t);
console.log('ta1 = '+ ta);
}
The result is:
[1, 2, 3, 4, 5, 6, 7, 8, 18]
"ta = 1,2,3,4,5,6,7,8"
"ta1 = 1,2,3,4,5,6,7,8,18"
[1, 2, 3, 4, 5, 6, 7, 8, 18]
You can find its jsfiddle at here http://jsfiddle.net/hm9KN/
Can anyone explain why the value of array changed even before I make any changes to it?
Thank you